給gridview動態生成radiobutton添加OnCheckedChanged事件

在寫研究生系統審覈功能的時候,有一個頁面的gridview動態生成四列radiobutton,在gridview控件外有四個radiobutton分別控制這四列的全選中。那麼當外面的某個全選按鈕選中後,改變gridview裏面的radiobutton選擇,對應的全選按鈕選中狀況應該發生變化。爲了實現該功能,須要在寫好了radiobutton的OnCheckedChanged監聽函數並添加到aspx頁面後,再在gridview的RowDataBound事件中加入聲明。 html

aspx頁面部分代碼: 函數

 

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

加入聲明代碼以下://給每一個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)
        { }
spa

OnCheckedChanged函數: code

#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
server

相關文章
相關標籤/搜索