我没有在网格视图中获取所选行的ID值? [英] I don;t get the value of ID of the selected row in grid view?

查看:101
本文介绍了我没有在网格视图中获取所选行的ID值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到DataTable的GridView,我试图选择并获取ID但该方法不起作用,我没有得到任何异常,但即使我尝试打印其中的任何其他文本方法我没有得到任何东西。



如果有基于asp和c#的其他解决方案根据行进行选择并获取ID请告诉我。



I have a GridView which is bind to a DataTable, I tried to make select and get the ID but the method is not working, I don;t get any exception, but even when I try to print any other text inside the method I got no thing.

If there is any other solutions based on asp and c# to make selection based on row and get the ID please tell me it.

<asp:GridView ID="grid" runat="server" Width="400" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField HeaderText="ID" DataField="ID" />
    <asp:BoundField HeaderText="Title" DataField="Title" />
<asp:TemplateField>
<ItemTemplate>
    <asp:CheckBox ID="CheckBox1" Text="text" runat="server" OnCheckedChanged="Check_Clicked" val='<%#Eval("ID")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><br />










protected void Check_Clicked(object sender, EventArgs e)
    {
        TextBox1.Text=(sender as CheckBox).Attributes["val"];
    }

推荐答案

您应该使用数据密钥名称。参考:示例 [ ^ ]
Your should use data key name. Refer:example[^]


protected void Check_Clicked(object sender, EventArgs e)
  {
      CheckBox chk1 = (CheckBox)sender;
      GridViewRow gr = (GridViewRow)chk1.Parent.Parent;
      Label lblactivityid = (Label)gr.FindControl("lblActivityid");
      Response.Write(lblactivityid.Text)
     //You can find Datakey of Gridview too like this--
     string ID=GridView1.DataKeys[gr.RowIndex].Value.ToString();
     Response.Write(ID);
  }







instead of Binding value to checkbox keep it as label Text and make this label to visible false then on check change event you can find the Label value which will be selected Row Value or you can use Datakey concept.


这篇关于我没有在网格视图中获取所选行的ID值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆