【Asp .Net C#】Checkbox 選擇全部用法

Asp .Net C#

選擇全部用法

在  aspx 頁加入

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <script language="javascript">
    function CheckAllItem(Check) {
        elm = document.forms[0];  //取得form表單

        for (i = 0; i < elm.length; i++) {
            if (elm[i].type == "checkbox" && elm[i].id != Check.id) //若為checkbox,並且ID與表頭CheckBox不同。表示為明細的CheckBox
            {
                if (elm.elements[i].checked != Check.checked)  //若明細的CheckBox的checked狀態與表頭CheckBox不同
                {
                    elm.elements[i].click();  //明細的CheckBox執行click
                }
            }
        }
    }
</script>
</asp:Content>

在GridView 上設 TemplateField

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#003300" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource1" Width="568px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="選擇">
                    <HeaderTemplate>
                        全選/取消
                        <asp:CheckBox ID="CheckAllItem" runat="server" onclick="javascript: CheckAllItem(this);"  />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" />
                    <ItemStyle HorizontalAlign="Center"  />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="姓名" SortExpression="AgentName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("AgentName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="labEmpChiName" runat="server" Text='<%# Bind("AgentName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="員編" SortExpression="EmpID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("AgentID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="labEmpID" runat="server" Text='<%# Bind("AgentID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="目前未取號件數"></asp:TemplateField>
                <asp:BoundField DataField="tkcenter" HeaderText="地區" SortExpression="tkcenter" />
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#3333CC" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>

Button按下後,判斷選擇的項目


    protected void BtnSendOK_Click(object sender, EventArgs e)
    {
        //現在時間
        DateTime dtnow = DateTime.Now;
        try
        {
            if(ddlAssCount.SelectedValue.ToString() !="0")
            {
                SqlConnection SqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ENERGYConnectionString"].ConnectionString);
                SqlConn.Open();
                {
                    int i = 0;
                    GridViewRow grv;
                    for (i = 0; i < GridView1.Rows.Count; i++)
                    {
                        grv = GridView1.Rows[i];
                        string strEmpID = ((Label)grv.FindControl("labEmpID")).Text.Trim();
                        string strEmpChiName = ((Label)grv.FindControl("labEmpChiName")).Text.Trim();

                        if (((CheckBox)grv.FindControl("CheckBox1")).Checked == true)
                        {
							//所選的項目要做的程式==========
                        }
                    }

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('已分派完成');window.location.href='EngKeyinAssign.aspx';", true);
                }
                SqlConn.Close();
                SqlConn.Dispose();
            }


        }
        catch (Exception E)
        {
            lblErrorShow.Text += E.ToString() + "</BR>";
        }
    }