Question about DataList

J

jobo

Hello,

I'm trying to assign a NavigateURL in my HyperLink in my DataList from
the backend. Here's my code:

<asp:DataList id="ItemsList"
OnItemDataBound="oidb"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
BorderStyle="none"
runat="server">

<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>

<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>

<HeaderTemplate>

List of items

</HeaderTemplate>

<ItemTemplate>

<asp:HyperLink ID="hLink"
runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "FriendlyName") %>'
/>

</ItemTemplate>

</asp:DataList>

How do I access the NavigateURL from the backend? Should I assign the
NavigateURL on the OnItemDataBound? Thanks.
 
G

Guest

Howdy,

I am not sure what you mean by 'backend' - does it mean code behind,
predefined value which can be set in the administration panel?

1. The same way you're setting the text property:

<asp:HyperLink ID="hLink" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "FriendlyName") %>'
NavigateUrl='<%# GetUrlExpression %>' />

where GetUrlexpression is a function, property or variable that returns url,
i.e (c# code behind)
protected string GetUrl
{
get
{
return "http://www.microsoft.com"; // just an example :D
}
}

2. use itemdatabound event handler

protected void oidb(object sender, DataListItemEventArgs e)
{
DataListItem item = e.Item;

if (item.ItemType == ListItemType.Item ||
item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hyperLink = (HyperLink) item.FindControl("hLink");
if (hyperLink != null)
{
hyperLink.NavigateUrl = "http://www.microsoft.com";
}
}
}


hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top