Get a hyperlink control inside a datalist when databouding

A

Arjen

Hello,

Here is my datalist.
<asp:DataList id="myDataList" CellPadding="4" Width="100%" runat="server">
<ItemTemplate>
<span class="Normal">
<asp:HyperLink id="link" Text='<%# Server.HtmlEncode((String)
DataBinder.Eval(Container.DataItem,"Title")) %>' NavigateUrl='<%#
DataBinder.Eval(Container.DataItem,"Url") %>' ToolTip='<%#
DataBinder.Eval(Container.DataItem,"Description") %>' runat="server" />
</span>
<br>
</ItemTemplate>
</asp:DataList>

I want to add to this hyperlink control the property target.
Some links should open inside a new window and some other not.

How can I do this?

I have tried this but it doesn't work.
private void myDataList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e) {
DataRowView drv = (DataRowView) e.Item.DataItem;
HyperLink link = (HyperLink) e.Item.FindControl("link");

if (Convert.ToBoolean(drv["NewWindow"]) == true){
link.Target = "_blank";
}
else {
link.Target = "_self";
}
}

Can somebody help me?

The error message was.
System.InvalidCastException: Specified cast is not valid.
On this line.
DataRowView drv = (DataRowView) e.Item.DataItem;

Thanks!
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top