views:

610

answers:

2

My question is fairly simple. This is what I have for the aspx page:

<ul>
    <asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
    <ItemTemplate>
        <li><asp:HyperLink runat="server" ID="link" /></li>
    </ItemTemplate>
    </asp:Repeater>
</ul>

I'm trying to get a list of hyperlinks from a SQL server into a list. This is what I have in the codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = Utilities.RunSelectQuery("SELECT *");
    DataTable dt = ds.Tables[0];

    linksList.DataSource = dt;
    linksList.DataBind();
}

How do I change the NavigateUrl and Text properties in the asp:HyperLink after data's been bound to the Repeater? I want to do this in the codebehind, I can get it to work if I do it using <%# Eval("URL") %> in the aspx page but that's sort of against what ASP.NET is all about.

Edit: this is the solution that worked for me thanks to womp:

protected void linksList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
        HyperLink link = (HyperLink)e.Item.FindControl("link");

        link.Text = row["description"].ToString();
        link.NavigateUrl = row["URL"].ToString();
    }
}
+2  A: 

Actually, using the Databinder syntax in your templates is a great way to do it, I'm not sure what you mean that it's "against what ASP.Net is all about".

However, if you really want to do it in code, you can do it right in your OnItemDataBound handler (which it looks like you've created). Something like this (read: untested) should do the trick:

void linksList_OnItemDataBound(object Sender, RepeaterItemEventArgs e) 
{

        if (e.Item.ItemType == ListItemType.Item 
              || e.Item.ItemType == ListItemType.AlternatingItem) {

             DataRow row = e.Item.DataItem as DataRow;
             Hyperlink link = e.Item.FindControl("link"));
             link.Text = row["URL"];
             link.NavigateUrl = row["URL"];
             }
          }
       }
womp
Ah thanks, that's exactly what I needed. The reason why I need to know how to do the codebehind method is because I have more complicated examples that requires manipulating data rather than just pulling it straight from a SQL table. I updated my original post with the correctly-implemented method, what you gave me almost works correctly, it just needed a few casts.
Daniel T.
+1  A: 

The way to do it is just as you said. Doing it in the code behind adds unnecessary work if you only wish to display the URL. You also would want to put the tags in the HeaderTemplate and FooterTemplate.

    <asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate>
        <li><asp:HyperLink runat="server" ID="link" NavigateUrl='<%# Eval("url") %>' /></li>
    </ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
    </asp:Repeater>
Ronnie
Is there any advantage to putting the tags in the header and footer templates instead of out? I'm trying to keep the code as readable as possible for the HTML coders here.
Daniel T.
Yes, say you hide the grid in your code behind, you'll then just have <ul></ul> sitting there. You might hide the grid if your query returns 0 records. When you put the <ul> in your headertemplate, it'll hide that too.
Ronnie
Oh good point, I didn't think of that.
Daniel T.