简体   繁体   English

如何在ASP.net C#中选择数据网格中的单元格

[英]How to select a cell in datagrid onClick in ASP.net C#

I am importing a column in datatable to my grid. 我正在将数据表中的列导入到我的网格中。 Now I want navigate to a new page on selecting a cell in grid by fetching the selected value. 现在我想要通过获取所选值来导航到选择网格中单元格的新页面。 I have tried this by including bound field in my grid like 我通过在我的网格中包含绑定字段来尝试这个

 <asp:GridView ID="GDV_1" runat="server" EnableModelValidation="true" AutoGenerateColumns="false" OnSelectedIndexChanged="GDV_1_SelectedIndexChanged" AutoGenerateSelectButton="false" DataKeyNames="SROID">
      <Columns>
         <asp:BoundField DataField="SRONameinEnglish" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}>ClickHere</a>" />
      </Columns>
 </asp:GridView>

Doing this way my requirement is achieved but the all cells are displaying Common text Click here instead of showing data from Database. 这样做我的要求,但所有单元格都显示公共文本Click here而不是显示数据库中的数据。

Please give your suggestion on how to get the value from database into cell and make it clickable . 请提出您如何从数据库获取值到单元格并使其clickable I don't want to use Select button . 我不想使用“ Select button Please find my current output. 请找到我目前的输出。 This is my current output I want my data from DB instead of ClickHere . 这是我当前的输出我希望我的数据来自DB而不是ClickHere

在此输入图像描述

You can use TemplateField 您可以使用TemplateField

<asp:TemplateField HeaderText="Name">
        <ItemTemplate>
            <asp:LinkButton runat="server" ID="lnk<%# Eval("SRONameinEnglish")%>"><%# Eval("SRONameinEnglish")%></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

and click of LinkButton put your code to navigate anywhere. 并单击LinkBut​​ton将您的代码放在任何地方导航。

In your case you are binding boundfield with static a tag which have href attribute so your not able to change text on that boundfield from your database.To get your approach you should use TemplateField and bind data with text attribute using eval keyword as below example. 在您的情况下,您使用具有href属性的静态a tag绑定boundfield ,因此您无法从数据库更改该boundfield上的文本。要获得您的方法,您应该使用TemplateField并使用eval keyword将数据绑定到text属性,如下例所示。

Try it: 试试吧:

<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("name") %>' />
    </ItemTemplate>
</asp:TemplateField>

OR 要么

you can also bind link with your hyperlink using NavigateUrl property of hyperlink as below example. 您还可以使用超链接的NavigateUrl属性将链接与您的超链接绑定,如下例所示。

<asp:TemplateField HeaderText="Name">
      <ItemTemplate>
          <asp:HyperLink id="HyperLink2" NavigateUrl='<%#Eval("YourUrl") %>' Text='<%#Eval("name") %>' runat="server"/>
      </ItemTemplate>
</asp:TemplateField>

I hope it will helpful to you. 我希望它对你有所帮助。

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

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