Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

XML
<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 />



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

Your should use data key name. Refer:example[^]
 
Share this answer
 
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900