I have a GridView that displays a hyperlink. I need the hyperlink to point to one page if ClaimType = "P" and to a different page if ClaimType = "I".
I tried doing this NavigateUrl = "<%# CreateLink(Eval('ClaimId'), Eval('ClaimType'))%>" but it gave me this error Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField
does not have a DataBinding event.
Can anyone offer some help as to how I can create the URL based on ClaimType being "P" or "I". In the old DataGrid I would use ondatabound, find the control and then write the URL at that point. I don't see the OnDataBound event for the GridView
though.
Does this how you reference the entire row and all the columns in that row?
Also in the line ((Hyperlink)e.Row.FindControl("lnkUrk")).NavigateUrl = 'one url';
What is the "InkUrk" is this the ID of the Hyperlink? As I see no way to give my <asp:HyperLinkField> an ID by which to reference it.
Yes the above refer the whole row (BTW the example i gave is bound to a Custom Object instead of a DataRow)
I assumned the link is in Template Column instead of DataBound. You can change it to following:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkUrl" runat="server" NavigateUrl="YourUrl"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Smitty200
Member
86 Points
36 Posts
needs dynamic URL creation
Jan 29, 2007 05:09 PM|LINK
I have a GridView that displays a hyperlink. I need the hyperlink to point to one page if ClaimType = "P" and to a different page if ClaimType = "I".
I tried doing this NavigateUrl = "<%# CreateLink(Eval('ClaimId'), Eval('ClaimType'))%>" but it gave me this error Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.
Can anyone offer some help as to how I can create the URL based on ClaimType being "P" or "I". In the old DataGrid I would use ondatabound, find the control and then write the URL at that point. I don't see the OnDataBound event for the GridView though.
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: <asp:HyperLinkField> needs dynamic URL creation
Jan 29, 2007 05:23 PM|LINK
Hook the RowDataBound event of the GridView, For ex:
protected void ListRowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row != null) && (e.Row.RowType == DataControlRowType.DataRow))
{
Order order = e.Row.DataItem as Order; // Assuming the GridView
if (order != null)
{
if (order.ClaimType == "P")
{
((Hyperlink)e.Row.FindControl("lnkUrk")).NavigateUrl = 'one url';
}
else if (order.ClaimType == "I")
{
((Hyperlink)e.Row.FindControl("lnkUrk")).NavigateUrl = 'another url';
}
}
}
}
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
Smitty200
Member
86 Points
36 Posts
Re: needs dynamic URL creation
Jan 29, 2007 05:36 PM|LINK
OK. So the line where you say
Order order = e.Row.DataItem as Order;
Does this how you reference the entire row and all the columns in that row?
Also in the line ((Hyperlink)e.Row.FindControl("lnkUrk")).NavigateUrl = 'one url';
What is the "InkUrk" is this the ID of the Hyperlink? As I see no way to give my <asp:HyperLinkField> an ID by which to reference it.
This is my html code for the gridview
<asp:GridView ID="grdClaims" runat="server" AllowPaging="True" PageSize="20" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="Azure"> <Columns> <asp:HyperLinkField HeaderText="Claim ID" DataTextField="ClaimId" /> <asp:BoundField HeaderText="Member Last Name" DataField="MemLastName" /> <asp:BoundField HeaderText="Member First Name" DataField="MemFirstName" /> <asp:BoundField HeaderText="Member ID" DataField="MemberId" /> <asp:BoundField HeaderText="Insert Date" DataField="InsertDate" /> </Columns> </asp:GridView>KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: needs dynamic URL creation
Jan 30, 2007 04:41 PM|LINK
Yes the above refer the whole row (BTW the example i gave is bound to a Custom Object instead of a DataRow)
I assumned the link is in Template Column instead of DataBound. You can change it to following:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkUrl" runat="server" NavigateUrl="YourUrl"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
rachaelov
Member
2 Points
5 Posts
Re: needs dynamic URL creation
Nov 19, 2009 09:45 PM|LINK
A bit late, but there is this solution
<asp:HyperLinkField HeaderText="Surname" DataNavigateUrlFields="id" DataNavigateUrlFormatString="Person.aspx?id={0}" DataTextField="surname"> </asp:HyperLinkField>where {0} is replaced by whatever the value of id is for that data row.