How to add hyperlink to boundfield in gridview c# asp.net

user2994144 picture user2994144 · Jan 20, 2014 · Viewed 54.8k times · Source

I have a gridview in aspx page, I need it to go add hyperlink to the Component from BoundField once the user clicks on the Component1 value. How can I add the hyperlink to boundfield that's related to BoundField?

<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
        DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
        <FooterStyle BackColor="#CCCC99" />
        <Columns>

<asp:BoundField DataField="Component1"  ItemStyle-Font-Size="Small" HeaderStyle-Width="80px"  HeaderStyle-Font-Size ="Medium" SortExpression="Component1"   /> 

</Columns>
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"  />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White"  />
    </asp:GridView>

Answer

Victor_Tlepshev picture Victor_Tlepshev · Apr 2, 2014

Try this.

 <asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
    BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
    <FooterStyle BackColor="#CCCC99" />
 <Columns>
  <asp:HyperLinkField DataNavigateUrlFields="StockNumber" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}" DataTextField="StockNumber" />
 </Columns>
 <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"  />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White"  />
 </asp:GridView>

To pass more than one variable, do this.

 <asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
    BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
    <FooterStyle BackColor="#CCCC99" />
 <Columns>
  <asp:HyperLinkField DataNavigateUrlFields="StockNumber, ID, CITY" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}&id={1}&CITY{2}" DataTextField="StockNumber" />
 </Columns>
 <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"  />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White"  />
 </asp:GridView>

DataNavigateUrlFields - The fields you would like to pass using the hyperlink column.

DataTextField - Current display field in the DatagridView.

HeaderText - header text which should be the description of the DataTextField value.