gridview和CheckBox結合實現多選,並用用table顯示出來。

來源:互聯網
上載者:User

主要是實現用checkbox選擇,

用gridview顯示出來後,我選擇幾個checkbox後點擊申請在後台出現如下介面,

我上面點了兩個,後面就用Table顯示出兩條記錄。

1.首先在Gridview中添加個複選框。

添加複選框代碼

<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckBoxALL" runat="server" AutoPostBack="True" oncheckedchanged="HeaderCheckBoxALL_CheckedChanged" />
</HeaderTemplate>
<HeaderStyle Font-Size="9pt" Width="20px" />
</asp:TemplateField>

後台增加複選框的選擇功能 雙擊申請的那個按鈕後。

 protected void btsq_Click(object sender, EventArgs e)
{

  //用IList儲存選擇了哪個複選框。
IList<string> zcbmList = new List<string>();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)(GridView1.Rows[i].FindControl("CheckBox1"));
if (cbox.Checked == true)
{
                //用迴圈查出來哪個複選框選中,並把這個行的第二列的值存到IList中。我這裡第二列是唯一標示列        
zcbmList.Add(GridView1.Rows[i].Cells[2].Text);


}

}

         //用Session傳遞值。
Session["zcbmList"] = zcbmList;
Response.Redirect("shenqing.aspx");
return;
}

選擇事件加完後,就到shenqing.aspx介面去接受Session

public partial class shenqing : ModulePage
{
IList<string> zcbmList = null;
protected void Page_Load(object sender, EventArgs e)
{
//接受前面介面傳遞過來的SESSION值
zcbmList = Session["zcbmList"] as List<string>;
if (!IsPostBack)
{
string sql1 = "select 資產編碼, 裝置編碼,裝置名稱,規格型號,自編號,投產日期,生產廠家,資產原值,帳麵價值,備忘,使用方式 from 裝置基礎資料表 where ";
        //用foreach語句和拼接功能實現對複選框選擇傳遞過來的值進行遍曆。
foreach (var zcbm in zcbmList)
{
if (zcbmList.IndexOf(zcbm) < zcbmList.Count - 1)
{
sql1 += "資產編碼='" + zcbm + "' or ";
}
else
{
sql1 += "資產編碼='" + zcbm + "'";
}

}

DataTable table = OracleHelper.ExecuteDataset(base.Conn, CommandType.Text, sql1).Tables[0];
        //把table儲存上,要不你沒回重新整理頁面後這個Table就會為空白,出現錯誤。在前台在綁定。
ViewState["table"] = table;

}
}

後台基本上就如上面所示了。接下來就要遍曆一下前台。遍曆<tr>。 <asp:textbox>要改成<input type="text">

 <% foreach (System.Data.DataRow row in ((System.Data.DataTable)ViewState["table"]).Rows)
{ %>
<tr>
<td class="style65">
<input type="text" ID="tbzcbm " class="SmallInput" ReadOnly ="true" Width="118px" value="<%= row["資產編碼"] %>" />
</td>
<td class="style13">
<input type="text" ID="tbzcmc" class="SmallInput" ReadOnly ="true" value ="<%=row["裝置名稱"] %>"></input>
</td>
<td class="style41">
<input type="text" ID="tbzcxh" class="SmallInput" ReadOnly ="true" value ="<%=row["規格型號"] %>"></input>
</td>
<td class="style42">
<input type="text" ID="tbzbh" class="SmallInput" ReadOnly ="true" value ="<%=row["自編號"] %>"></input>
</td>
<td class="style16">
<input type="text" ID="tbtcrq" class="SmallInput" ReadOnly ="true" value="<%=row["投產日期"] %>"></input>
</td>
<td class="style43">
<input type="text" ID="tbsccj" class="SmallInput" ReadOnly ="true" value ="<%=row["生產廠家"] %>"></input>
</td>
<td class="style44">
<input type="text" ID="tbyz" class="SmallInput" ReadOnly ="true" value ="<%=row["資產原值"] %>"></input>
</td>
<td class="style19">
<input type="text" ID="tbzmjz" class="SmallInput" ReadOnly ="true" value ="<%=row["帳麵價值"] %>"></input>
</td>
<td class="style74">
<input type="text" ID="tbbz" class="SmallInput" ReadOnly ="true" value="<%=row["備忘"] %>"></input>
</td>
</tr>
<%}%>

這樣就實現了不用Gridview 用table來動態產生表單。

要是不理解大家可以加QQ59984296.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.