Hyperlinks inside the DataList, Datagrid or GridView control

Sometimes we need to get some id from the database table and use that id to generate the link. Something like this:

SomePage.aspx?id=34

You can easily do this by using the following code:

<ItemTemplate>
<asp:HyperLink ID="myHyperlink"
 Text=
'<%# Eval("CategoryName") %>'
 
NavigateUrl='<%# FormatUrl( (int) Eval("CategoryID")) %>'
 
runat="server">[myHyperlink]</asp:HyperLink>
       </ItemTemplate>

And here 
is the FormatUrl method:

 
protected string FormatUrl(int categoryID)
    {
        
return "SomePage.aspx?" + categoryID;
    }

This will create links inside the DataList control. You can try similar approach using Datagrid or GridView controls.

 

powered by IMHO 1.2

Print | posted @ Thursday, December 22, 2005 5:52 PM

Twitter