Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange problem...? 1

Status
Not open for further replies.

andym0908

Programmer
Feb 25, 2008
19
0
0
GB
I've just started using .NET (C#). I've got a simple horizontal text display, using the <asp:HyperLink> control, which is databound to a SQL Stored proc.

When I run it, the text displays, though only as normal text, not Hyperlink text. Also, more worryingly, when I View Source in the browser (IE 7), the actual code for the HyperLink code is showing up (ie. it shows <asp:HyperLink> instead of <a>). Why would it not render?!

 
For the hyperlink to actually be a hyperlink, you have to set the NavigateURL property.
 
Can you paste into the thread.

1. the asp block containing the hyperlink object.

2. The same from the view source.

-- Jason
"It's Just Ones and Zeros
 
The NavigateURL property is set...
Also, I have pasted the code below:


<asp:Repeater ID="rptSiteMenu" runat="server">

<ItemTemplate>
<asp:HyperLink
Text="<%#DataBinder.Eval(Container.DataItem,"title")%>"
NavigateUrl="<%#DataBinder.Eval(Container.DataItem,"url")%>"
ID="<%# "menu_"+DataBinder.Eval(Container.DataItem,"id")%>"
Name="<%# "menu_"+DataBinder.Eval(Container.DataItem,"id")%>"
runat="server">
<%#DataBinder.Eval(Container.DataItem,"title")%></asp:HyperLink>
</ItemTemplate>

<SeparatorTemplate>|</SeparatorTemplate>

</asp:Repeater>
 
Oh, here's the View Source ... also, I'm using MasterPages - that shouldn't have any effect should it!?!

<div style="text-align:center">

header

<br/>


<asp:HyperLink
Text="Home"
NavigateUrl="default.aspx"
ID="menu_1"
Name="menu_1"
runat="server">
Home</asp:HyperLink>
|
<asp:HyperLink
Text="What Is This?"
NavigateUrl="wii.aspx"
ID="menu_2"
Name="menu_2"
runat="server">
What Is This?</asp:HyperLink>
|
<asp:HyperLink
Text="Galleries"
NavigateUrl="galleries.aspx"
ID="menu_6"
Name="menu_6"
runat="server">
Galleries</asp:HyperLink>
|
<asp:HyperLink
Text="Support"
NavigateUrl="support.aspx"
ID="menu_7"
Name="menu_7"
runat="server">
Support</asp:HyperLink>


footer

</div>
 
you cannot dynamically assign the id property this way. try this instead
Code:
<asp:Repeater ID="rptSiteMenu" runat="server">
   <ItemTemplate>
      <asp:HyperLink
         Text='<%#DataBinder.Eval(Container.DataItem, "title")%>'
         NavigateUrl='<%#DataBinder.Eval(Container.DataItem, "url")%>'
         ID="link"
         runat="server" />
   </ItemTemplate>
   <SeparatorTemplate>|</SeparatorTemplate>
</asp:Repeater>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hmm fair point which I didn't know. However, the change doesn't make a difference to the problem I'm having...
 
if you're seeing server tags on the client the problem is outside the scope of the posted posted. somewhere you are trafering the literal text, instead of compiling the code into html.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Ok, where do I go to change these properties? This is really strange, because this is a brand new project, very simple (so far).

Why on earth would the server tags render on the client end..?

FYI, the code-behind code (below) is used to just bind a stored procedure (via data Table Adapter) to the control:-

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mrp1TableAdapters;


public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
		mrp1TableAdapters.mrp_menuTableAdapter mrp_menuAdapter =
			new mrp_menuTableAdapter();
		rptSiteMenu.DataSource = mrp_menuAdapter.GetMenu();
		rptSiteMenu.DataBind();
    }
}
 
In the repeater's ItemDataBound event, you first need to get a reference to the hyperlink. Then, you can assign the properties from there.
 
I'm puzzled by this, it should be a piece of cake!

The Repeater control is bound to the stored proc, according to the code behind... Why would I need to get a reference to the hyperlink? This doesn't seem to be a reason why the server tag is showing on the client end source...

Please check this Repeater tutorial, as this is what I used to write the code.


 
This doesn't seem to be a reason why the server tag is showing on the client end source...
No, you're right, it probably isn't. However, you haven't provided a complete example of your page that demonstrates the issue and would allow us to replicate your problem.



-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Ok, here's my HTML:

Code:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title>Test</title>
    <link rel="stylesheet" href="mrpcss.css" type="text/css" />
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>


<form id="pageform" runat="server">

<div style="text-align:center">

    header
    
    <br/>
    
    <asp:Repeater ID="rptSiteMenu" runat="server">
        <ItemTemplate>
            <asp:HyperLink
                Text="<%#DataBinder.Eval(Container.DataItem,"title")%>"
                NavigateUrl="<%#DataBinder.Eval(Container.DataItem,"url")%>"
                ID="link"
                runat="server">
                <%#DataBinder.Eval(Container.DataItem,"title")%></asp:HyperLink>
        </ItemTemplate>
        <SeparatorTemplate>|</SeparatorTemplate>
    </asp:Repeater>  

    <div>
        <asp:ContentPlaceHolder id="pageBody" runat="server"></asp:ContentPlaceHolder>
    </div>
  
footer
    
</div>

</form>

</body>
</html>

And the code behind:
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mrp1TableAdapters;


public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
		mrp1TableAdapters.mrp_menuTableAdapter mrp_menuAdapter =
			new mrp_menuTableAdapter();
		rptSiteMenu.DataSource = mrp_menuAdapter.GetMenu();
		rptSiteMenu.DataBind();
    }
}

I've included the TSQL code as it definately returns the correct data...
 
Actually, I just realised that jmeckley has already provided you the correct solution. Notice the subtle differences in what he told you to try:
Code:
<asp:Repeater ID="rptSiteMenu" runat="server">
   <ItemTemplate>
      <asp:HyperLink
         Text=[!]'[/!]<%#DataBinder.Eval(Container.DataItem, "title")%>[!]'[/!]
         NavigateUrl=[!]'[/!]<%#DataBinder.Eval(Container.DataItem, "url")%>[!]'[/!]
         ID="link"
         runat="server" />
   </ItemTemplate>
   <SeparatorTemplate>|</SeparatorTemplate>
</asp:Repeater>


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Ahh.. I kinda missed that - using ' instead of " ! I think I did notice it, but it didn't register to me!

Much gracious to you Mark, and Jason!!
 
Just one other thought.

Why use a hyperlink control.
Why not just use an "a" tag.

E.G.
Code:
<asp:Repeater ID="rptSiteMenu" runat="server">
   <ItemTemplate>
     <a href='<%#DataBinder.Eval(Container.DataItem, "url")%>'><%#DataBinder.Eval(Container.DataItem, "title")%></a>
   </ItemTemplate>
   <SeparatorTemplate>|</SeparatorTemplate>
</asp:Repeater>

It doesn't make too much sense to use a server side control if you need client side functionality.

Rob
 
Hi Rob

Well, good point - but I'm just getting into .NET (after a long period of intimidation by it!).

What's the use, then, of a HyperLink control?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top