zoukankan      html  css  js  c++  java
  • DataList、Repeater、GridView中的Checkbox取值问题

    先看页面代码

     1 <asp:DataList id="DataList1" runat="server" Width="100%" RepeatColumns="4" Font-Size="10pt">
     2         <ItemTemplate>
     3          <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" bgColor="gainsboro" border="0">
     4           <TR>
     5            <TD align="center">
     6             <asp:Label id=lblOName runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_name")%>'>
     7             </asp:Label>
     8             <asp:Label id=lblID runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_id")%>' Visible="False">
     9             </asp:Label></TD>
    10           </TR>
    11           <TR>
    12            <TD align="center">
    13             <asp:CheckBoxList id="cbSelect" runat="server" Font-Size="10pt" RepeatColumns="2" Width="100%" BackColor="#FFE0C0"
    14              RepeatDirection="Horizontal" DataTextField="nmenu_tname" DataValueField="nmenu_tid"></asp:CheckBoxList></TD>
    15           </TR>
    16          </TABLE>
    17         </ItemTemplate>
    18        </asp:DataList>

    后台绑定CheckBox:

     1 private void Page_Load(object sender, System.EventArgs e)
     2   {
     3    // 在此处放置用户代码以初始化页面
     4    if(!IsPostBack)
     5    {
     6     if(Session["UID"]!=null&&Session["UID"].ToString()!="")
     7     {
     8      BindList();
     9      BindCheckBox();
    10     }
    11     else
    12     {
    13      Server.Transfer("error.aspx");
    14     }
    15    }
    16   }
    17 private void BindList()
    18   {
    19    string sql = "select * from NewsOne";
    20    DataList1.DataSource = ort.ExecuteToTable(sql);
    21    DataList1.DataBind();
    22   }
    23 private void BindCheckBox()
    24   {
    25    foreach(DataListItem dli in DataList1.Items)
    26    {
    27     CheckBoxList cbl = (CheckBoxList)dli.FindControl("cbSelect");
    28     Label lbl = (Label)dli.FindControl("lblID");
    29     string sql = "select * from NewsTwo where nmenu_oid="+lbl.Text;
    30     cbl.DataSource = ort.ExecuteToTable(sql);
    31     cbl.DataBind();
    32    }
    33   }

    CheckBox取值:

     1 string list = "";
     2      foreach(DataListItem dli in DataList1.Items)
     3      {
     4       CheckBoxList cbSel = (CheckBoxList)dli.FindControl("cbSelect");
     5       for(int i=0;i<cbSel.Items.Count;i++)
     6       {
     7        if(cbSel.Items[i].Selected == true)
     8        {
     9         list += cbSel.Items[i].Value + ",";
    10        }
    11       }
    12      }
    13      list = list.Trim(",".ToCharArray());

    有的时候会出现取不到值的情况,我的看法是缺少if(!IsPostBack)。

  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/Chaser-Eagle/p/3684846.html
Copyright © 2011-2022 走看看