点击 Button触发事件将GridView1 CheckBox勾选…

2018-11-26 07:57:47来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

有时候想实现一个CheckBox选取功能,但是很多细节不是很清楚

相信大家都有遇到类似的情况,直接看代码,如下:

前端代码GridView1,CheckBox控件设置

<asp:GridView ID="GridView1" runat="server" Height="2px" Width="720px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="1" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="False" PageSize="8" >
<RowStyle ForeColor="#000066" />
<FooterStyle BackColor="White" ForeColor="#000066" />

<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="false" />
<%--<asp:Label ID="lbformid" runat="server" Visible="false" Text='<%# Eval("formid") %>'></asp:Label>--%>
</ItemTemplate>
<HeaderTemplate>
<input type="checkbox" id="ChkHead" onclick="CheckAll3(this)" title="選擇全部" /><%--加上checked可自動勾選--%>
</HeaderTemplate>
<HeaderStyle Width="20px" />
<ItemStyle Width="20px" />

</asp:TemplateField>
                            <asp:BoundField DataField="data" HeaderText="日期(data)" ReadOnly="True" />
                            <asp:BoundField DataField="users" HeaderText="姓名(users)" SortExpression="姓名" />
                            <asp:BoundField DataField="user_name" HeaderText="賬號(user_name)"  />
                            <asp:BoundField DataField="user_email" HeaderText="郵箱(user_email)"  />
                            <asp:BoundField DataField="mony" HeaderText="金額(mony)" />

             </Columns>
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" CssClass="Freezing" />

 

 

 后台代码

protected void Button3_Click(object sender, EventArgs e)
{
GridView2.DataSource = null; //數據定義成空值
GridView2.DataBind();

if (GridView1.Rows.Count < 1) //GridView1控件數據小於1行,執行該語句
{
MessageBox.Text = "請先查詢資料再匯入";
MessageBox.ForeColor = System.Drawing.Color.Red; //獲取背景顏色
return;
}
DataTable dte = new DataTable();
dte.Columns.Add("data", typeof(string)); //獲取屬於該表列的集合
dte.Columns.Add("users", typeof(string));
dte.Columns.Add("user_name", typeof(string));
dte.Columns.Add("user_email", typeof(string));
dte.Columns.Add("mony", typeof(string));
DataRow dr;

////建立相同架構的新數據
//dr["data"] = "data";
//dr["users"] = "";
//dr["user_name"] = "";
//dr["user_email"] = "";
//dr["mony"] = "";
int j=0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked) //默認是true(勾起)
{
dr = dte.NewRow();//創建具有相同架構表的新數據
dr["data"] = GridView1.Rows[i].Cells[1].Text.ToString(); //獲取GridView1中列的位置,將數據綁定到建立的架構表相同的列
dr["users"] = GridView1.Rows[i].Cells[2].Text.ToString();
dr["user_name"] = GridView1.Rows[i].Cells[3].Text.ToString();
dr["user_email"] = GridView1.Rows[i].Cells[4].Text.ToString();
dr["mony"] = GridView1.Rows[i].Cells[5].Text.ToString();
dte.Rows.Add(dr);
j++;//增益性
}

}
if (j<=0) //當excel中的MHour(月加班上限)小於或等於GridView1控件中第5行時,將判斷“時數必須大於已用時數!!”
{
MessageBox.Text = dte + "請選擇一項";
MessageBox.ForeColor = System.Drawing.Color.Red;
return;
}

GridView2.DataSource = dte;
GridView2.DataBind();

 

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP.NET -- WebForm -- 页面生命周期事件

下一篇:怎樣添加設置GridView,CheckBox全選功能