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

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


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.


You won't be able to have a linkbutton in a bound field. However you can convert it to a TemplateField. Here is an example of my LinkButton.

<asp:TemplateField HeaderText="StockNumber" SortExpression="STOCK NO">      <ItemTemplate>           <asp:LinkButton ID="lbStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:LinkButton>      </ItemTemplate>      <HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Left" Width="80px" />      <ItemStyle HorizontalAlign="Left" /></asp:TemplateField>

To convert it to a TemplateField. Make sure you are in Design View. Then click on the smart tag. Edit Columns, select your column, then below the properties click "Convert to TemplateField"

EDIT: I just noticed you wanted a HyperLink instead of a LinkButton. You will still convert it the same way, but just put a HyperLink instead.

<asp:HyperLink ID="hlStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:HyperLink> 

Hope this helps!