简体   繁体   English

如何在gridview c#asp.net中添加超链接到boundfield

[英]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. 我在aspx页面中有一个gridview,我需要它在用户点击Component1值后从BoundField添加到Component的超链接。 How can I add the hyperlink to boundfield that's related to BoundField? 如何将超链接添加到与BoundField相关的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>

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. DataNavigateUrlFields - 您要使用超链接列传递的字段。

DataTextField - Current display field in the DatagridView. DataTextField - DatagridView中的当前显示字段。

HeaderText - header text which should be the description of the DataTextField value. HeaderText - 标题文本,应该是DataTextField值的描述。

You won't be able to have a linkbutton in a bound field. 您将无法在绑定字段中拥有链接按钮。 However you can convert it to a TemplateField. 但是,您可以将其转换为TemplateField。 Here is an example of my LinkButton. 这是我的LinkBut​​ton的一个例子。

<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. 将其转换为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" 编辑列,选择列,然后在属性下单击“转换为TemplateField”

EDIT: I just noticed you wanted a HyperLink instead of a LinkButton. 编辑:我刚刚注意到你想要一个HyperLink而不是一个LinkBut​​ton。 You will still convert it the same way, but just put a HyperLink instead. 你仍然会以相同的方式转换它,但只需改为使用HyperLink。

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM