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:
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:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
            <asp:CheckBox ID="CheckBox1" runat="server"/>

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

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

and then try
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";
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

Try this,

Modify Checkbox in Gridview like,

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

And on Code view define,

protected void chkview_CheckedChanged(object sender, EventArgs e)
     //Define your code here.
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