给gridview动态生成radiobutton添加OnCheckedChanged监听函数
在写研究生系统审核功能的时候,有一个页面的gridview动态生成四列radiobutton,在gridview控件外有四个radiobutton分别控制这四列的全选中。那么当外面的某个全选按钮选中后,改变gridview里面的radiobutton选择,对应的全选按钮选中情况应该发生变化。为了实现该功能,需要在写好了radiobutton的OnCheckedChanged监听函数并添加到aspx页面后,再在gridview的RowDataBound事件中加入声明。
aspx页面部分代码:
View Code
<asp:TemplateField HeaderText="学位类别" SortExpression="gdxw"> <ItemTemplate> <%# Eval("gdxw")%></ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="同意" SortExpression="xwkyqxf"> <ItemTemplate> <asp:RadioButton ID="TySy" runat="server" GroupName="bjyj" AutoPostBack="True" OnCheckedChanged="check_TySy" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="不同意" SortExpression="zdkxxf"> <ItemTemplate> <asp:RadioButton ID="BtySy" runat="server" GroupName="bjyj" AutoPostBack="True" OnCheckedChanged="check_BtySy"/> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="同意推迟" SortExpression="kkzyxkxf"> <ItemTemplate> <asp:RadioButton ID="TyTcsy" runat="server" GroupName="bjyj" AutoPostBack="True" OnCheckedChanged="chenk_TyTcsy" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="弃权" SortExpression="xwkyqcj"> <ItemTemplate> <asp:RadioButton ID="Qq" runat="server" GroupName="bjyj" AutoPostBack="True" OnCheckedChanged="chenk_Qq" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField>
加入声明代码如下:
View Code
//给每个RadioButton1绑定setRadio事件 try { ((RadioButton)e.Row.FindControl("TySy")).Attributes.Add("OnCheckedChanged", "chenk_TySy"); ((RadioButton)e.Row.FindControl("BtySy")).Attributes.Add("OnCheckedChanged", "chenk_BtySy"); ((RadioButton)e.Row.FindControl("TyTcsy")).Attributes.Add("OnCheckedChanged", "chenk_TyTcsy"); ((RadioButton)e.Row.FindControl("Qq")).Attributes.Add("OnCheckedChanged", "chenk_Qq"); } catch (Exception) { }
OnCheckedChanged函数:
OnCheckedChanged
#region 授予选择发生改变 //同意授予发生变化 protected void check_TySy(object sender, EventArgs e) { //记录同意授予项个数 int t = gvData.Rows.Count; for (int i = 0; i <= gvData.Rows.Count - 1; i++) { RadioButton TySy = (RadioButton)gvData.Rows[i].Cells[5].FindControl("TySy"); if (TySy.Checked == true) {//存在同意授予项 QbBty.Checked = false; QbTc.Checked = false; QbQq.Checked = false; } else { t--; } //Response.Write("<script>window.alert('进入同意授予发生变化方法。')</script>"); } if (t == gvData.Rows.Count) {//全为同意授予项 QbTy.Checked = true; } else {//存在非同意授予项 QbTy.Checked = false; } } //同意延迟授予发生变化 protected void chenk_TyTcsy(object sender, EventArgs e) { //记录同意推迟授予项 int t = gvData.Rows.Count; //循环个同意推迟授予项 for (int i = 0; i <= gvData.Rows.Count - 1; i++) { RadioButton TyTcsy = (RadioButton)gvData.Rows[i].Cells[7].FindControl("TyTcsy"); if (TyTcsy.Checked == true) {//存在一个同意延迟授予项 QbTy.Checked = false; QbBty.Checked = false; QbQq.Checked = false; } else { t--; } } if (t == gvData.Rows.Count) {//全为同意推迟授予项 QbTy.Checked = true; } else {//存在非同意延迟授予项 QbTy.Checked = false; } } //不同意授予发生变化 protected void check_BtySy(object sender, EventArgs e) { //记录不同意授予个数 int t = gvData.Rows.Count; //循环个不同意授予项 for (int i = 0; i <= gvData.Rows.Count - 1; i++) { RadioButton BtySy = (RadioButton)gvData.Rows[i].Cells[6].FindControl("BtySy");//取出个不同意授予项 if (BtySy.Checked == true) {//存在一个不同意授予项则将其余全选项置为false QbTy.Checked = false; QbTc.Checked = false; QbQq.Checked = false; } else { t--; } } // if (t == gvData.Rows.Count) {//全为不同意授予项 QbBty.Checked = true; } else {//存在非不同意授予项 QbBty.Checked = false; } } //弃权发生变化 protected void chenk_Qq(object sender, EventArgs e) { //记录选中个数 int t = gvData.Rows.Count; //循环查看个选项 for (int i = 0; i <= gvData.Rows.Count - 1; i++) { RadioButton Qq = (RadioButton)gvData.Rows[i].Cells[8].FindControl("Qq");//取出每一行的弃权项 if (Qq.Checked == true) {//如果存在一个弃权项,则所有的全选都置为false QbTy.Checked = false; QbBty.Checked = false; QbTc.Checked = false; } else { t--; } } if (t == gvData.Rows.Count) {//全部选择弃权 QbQq.Checked = true; } else {//存在非弃权项 QbQq.Checked = false; } } #endregion