Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 613
user2994144 Avatar asked Jan 20 '14 20:01

user2994144


2 Answers

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!

like image 54
Humpy Avatar answered Oct 21 '22 21:10

Humpy


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.

like image 20
Victor_Tlepshev Avatar answered Oct 21 '22 19:10

Victor_Tlepshev