有時候想實現一個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();
}