如果不在datalist中添加linkbutton
那么怎么通过checkbox和一个不在datalist中的button来选中选定行并取值?
中间代码不知道咋写=.=     protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (DataListItem dli in this.DataList1.Items)       
        {
            CheckBox cb = (CheckBox)dli.FindControl("check");
            if (cb.Checked == true)
            {
                  
            }
        }    }

解决方案 »

  1.   


        ArrayList username = new ArrayList();
        protected void Button3_Click(object sender, EventArgs e)
        {
            
            foreach (DataListItem dli in this.DataList1.Items)       
            {
                CheckBox cb = (CheckBox)dli.FindControl("check");
                if (cb.Checked == true)
                {
                   Label Lab=(Label)this.DataList1.FindControl("username");
                   username.Add(Lab.Text.ToString());            }
            }    }这个也取不出值,有什么问题呢?
      

  2.   


    foreach (DataListItem dli in this.DataList1.Items)       
            {
                CheckBox cb = (CheckBox)dli.FindControl("check");
                if (cb.Checked == true)
                {
                   Label Lab=(Label)dli.FindControl("username");
                   username.Add(Lab.Text.ToString());            }
            }
      

  3.   

    我也发现了...
    现在新问题也来了 就是根本不管 CheckBox 选没选中都进不了if语句
    这个是什么问题
      

  4.   

    FindControl 里的ID 是否正确断点跟踪一下
      

  5.   

    datalist数据绑定的代码放用IsPostBack判断下。
      

  6.   

       <asp:CheckBox ID="check" runat="server"/>这个没错啊
    就是进不去if语句 下面这个全选CheckBox倒是可以执行 很是奇怪啊.. protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Button1.Text == "全选")
            {
                foreach (DataListItem dli in this.DataList1.Items)
                {
                    CheckBox cb = (CheckBox)dli.FindControl("check");
                    cb.Checked=true;
                }
                this.Button1.Text = "全部取消";
            }
            else
            {
                foreach (DataListItem dli in this.DataList1.Items)
                {
                    CheckBox cb = (CheckBox)dli.FindControl("check");
                    cb.Checked = false;
                }
                this.Button1.Text = "全选";
            }
        }
      

  7.   

    页面载入的次序你没搞明白,当页面page_load后,datalist里面原本checkbox为true的都会没掉的,
      

  8.   


        protected void Page_Load(object sender, EventArgs e)
        {        if (!IsPostBack)
            {
                //指定datalist数据源并绑定
            }
        }
      

  9.   


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;public partial class cuser_UserC : System.Web.UI.Page
    {
        ArrayList username = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            BingData();
        }
        public void BingData()
        {
            DataAccess.Class1 dataClass = new DataAccess.Class1();
            this.DataList1.DataSource = dataClass.getTable(Common.Class1.sql_user, Common.Class1.table_user).Tables[0].DefaultView;
            this.DataList1.DataBind();
        }    protected void Button3_Click(object sender, EventArgs e)
        {
            foreach (DataListItem dli in this.DataList1.Items)       
            {
                CheckBox cb = (CheckBox)dli.FindControl("check");
                if (cb.Checked == true)
                {
                   Label Lab=(Label)dli.FindControl("username");
                   username.Add(Lab.Text.ToString());
                }
            }    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Button1.Text == "全选")
            {
                foreach (DataListItem dli in this.DataList1.Items)
                {
                    CheckBox cb = (CheckBox)dli.FindControl("check");
                    cb.Checked=true;
                }
                this.Button1.Text = "全部取消";
            }
            else
            {
                foreach (DataListItem dli in this.DataList1.Items)
                {
                    CheckBox cb = (CheckBox)dli.FindControl("check");
                    cb.Checked = false;
                }
                this.Button1.Text = "全选";
            }
        }
    前台 <asp:DataList ID="DataList1" runat="server" Height="167px" Width="754px" 
                        CaptionAlign="Left">
                        <ItemStyle Font-Size="X-Small" />
                        <HeaderStyle 
                Font-Names="宋体" Font-Size="Smaller" HorizontalAlign="Center" 
                VerticalAlign="Middle" />
                        <HeaderTemplate>
                            <table><tr>
         <td align="center" dir="ltr" 
         style="width:60px;  font-family: 宋体; font-size: smaller">
         选择</td>
         td align="center" style="width:80px; font-family: 宋体; font-size: smaller">
         用户名</td>
         <td align="center" style="width:100px; font-family: 宋体; font-size: smaller">
         设备号</td>
         <td align="center" style="width:100px; font-family: 宋体; font-size: smaller">
         设备数量</td>
     </tr>
     </HeaderTemplate>
     <ItemTemplate>
     <tr>
        <td><asp:CheckBox ID="check" runat="server"/>
        </td>
        <td><asp:Label ID="username" RUNAT="server" 
        TEXT= <%#DataBinder.Eval(Container.DataItem,"username") %>></asp:Label>
        <td>
        <asp:Label ID="deviceID" RUNAT="server" 
        TEXT= <%#DataBinder.Eval(Container.DataItem, "deviceID")%>></asp:Label>
        </td>
        <td><asp:Label ID="deviceNumber" RUNAT="server" 
        TEXT= <%#DataBinder.Eval(Container.DataItem, "deviceNumber")%>></asp:Label>
        </td>
    </tr>
                        </ItemTemplate>
                        <SeparatorTemplate>
                            <tr>
                            </tr>
                        </SeparatorTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:DataList>
       
      

  10.   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {             BingData();
            }
        }
      

  11.   

    你记得 操作服务器控件 引起回发 都会执行 Page_Load 并且 在 控件自己的事件前执行 就可以了。