Click here to Skip to main content
14,926,224 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
Iam having a grid which consists of check box. When user checks on any of the checkbox of the grid .I want to fire rowcommand event so that some database operation has to done based on the checkbox .rowcommand event is firing for only for button, but how can I do the same for the checkbox

example of my code is like this

ASP.NET
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server"/>
        </ItemTemplate>
</asp:TemplateField>
        </Columns>
        </asp:GridView>

thanks in advance.
Posted
Updated 10-Jan-12 1:21am
v2
Comments
m@dhu 10-Jan-12 7:21am
   
code block added.
Karthik Harve 10-Jan-12 7:22am
   
you can use check changed event....!!!!!!!!

Use
C#
<asp:checkbox id="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" xmlns:asp="#unknown" />

and then try
C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
lblmsg.Text = GridView1.DataKeys[gr.RowIndex].Value.ToString();

//lblmsg.Text = "Hello";
} 
   
Comments
palraj001 10-Jan-12 7:34am
   
add AutoPostBack=True in <asp:checkbox id="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" xmlns:asp="#unknown" />
Nigam Patel 10-Jan-12 7:50am
   
i agree with above solution:)
balasani raghav 25-Jul-12 8:20am
   
its working fine
raj ch 29-Mar-13 4:49am
   
thanks a lot it works for me
C#Programmer2012 24-Feb-14 9:17am
   
thank you,this work for me.
Member 11879748 18-Sep-15 7:52am
   
Multiple controls with the same ID 'CheckBox1' were found. FindControl requires that controls have unique IDs.
Maybe this will help. See the section on checkboxes

DataGridDemo[^]
   
Try this,

Modify Checkbox in Gridview like,

XML
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:CheckBox ID="chkview" runat="server" AutoPostBack="true" OnCheckedChanged="chkview_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>


And on Code view define,

C#
protected void chkview_CheckedChanged(object sender, EventArgs e)
{
     //Define your code here.
}
   
v2
Comments
lakshmichawala 10-Jan-12 8:20am
   
Thanks supriya its working
Gaurang from Ahmadabad 21-Aug-13 10:01am
   
but what happen at client side ?????????????
Supriya Srivastav 11-Jan-12 1:55am
   
@lakshmichawala Welcome dear...
Mark it as answered if your problem is solved.

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