Search code examples
asp.netgridviewhyperlinktemplatefield

asp.net hyperlinkfield set navigateurl "http=blabla/Id=" value from database


I am new to asp.net. Please help. I have 2 files. One default.aspx, one default.aspx.cs.

In the default.aspx, I have:

<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3366FF" HeaderStyle-ForeColor="White">
    <rowstyle backcolor="LightCyan" forecolor="Black" font-italic="true"/>

     <alternatingrowstyle backcolor="PaleTurquoise" forecolor="Black" font-italic="true"/>

    <Columns>
        <asp:BoundField ItemStyle-Width="15%" DataField="ID" HeaderText="ID" />

        <asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />

        <asp:BoundField ItemStyle-Width="15%" HeaderText="Delayed Delivery" />
    </Columns>
</asp:GridView>

My questions:

Column: hyperlink Log:

  1. I want to bind it to a column in database called "Base_Id", but when I use DataField="Base_Id" in the hyperlink definition, it gives error;

  2. I want to append the base_Id to the end of the url so that it gives navigateurl="http://somelink&RecordId=baseId_value" ; I tried TemplateField, but Visual Studio doesnot recognize it.

  3. for the delayed delivery, I need to compare the data and do some math. Is there a way for me to add a function in default.aspx.cs? if there's, could anyone give an example please?

Any ideas are appreciated. Thanks a lot. =)


Solution

  • Convert the column

      <asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
    

    to TemplateColumn and do this

    <asp:TemplateField HeaderText="Log" ItemStyle-Width="15%">
            <ItemTemplate>
                <asp:HyperLink runat="server" 
                    NavigateUrl='<%# GetUrl(Eval("Base_Id"))%>' 
                    text="Log"></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    

    and in code-behind

    protected string GetUrl(object id)
    {
    return "http://somelink&RecordId=" + id;
    }
    

    Hope this helps!

    Regards, Uroš