locked
<asp:HyperLinkField> needs dynamic URL creation RRS feed

  • Question

  • User651184261 posted

    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.

    Monday, January 29, 2007 1:09 PM

Answers

  • User1406895847 posted

    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';
       } 
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 29, 2007 1:23 PM

All replies

  • User1406895847 posted

    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';
       } 
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 29, 2007 1:23 PM
  • User651184261 posted

    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>
     
    Monday, January 29, 2007 1:36 PM
  • User1406895847 posted
    Order order = e.Row.DataItem as Order;

    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>
    Tuesday, January 30, 2007 12:41 PM
  • User-208600870 posted


    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.



    Thursday, November 19, 2009 5:45 PM