gridview复选框如何在选中事件时触发事件 [英] gridview checkbox how to fire an event when it is checked

查看:71
本文介绍了gridview复选框如何在选中事件时触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个包含复选框的网格.当用户选中网格的任何复选框时,我想触发rowcommand事件,以便必须根据复选框执行某些数据库操作.rowcommand事件仅针对按钮触发,但是我该怎么办复选框相同

我的代码示例就是这样

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: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.

推荐答案

使用
<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";
} 


也许会有所帮助.请参阅复选框部分

DataGridDemo [
Maybe this will help. See the section on checkboxes

DataGridDemo[^]


尝试一下,

在Gridview中修改复选框,例如,

Try this,

Modify Checkbox in Gridview like,

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



然后在代码"视图中定义,



And on Code view define,

protected void chkview_CheckedChanged(object sender, EventArgs e)
{
     //Define your code here.
}


这篇关于gridview复选框如何在选中事件时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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