我想批量提交datalist模板列里的数据,但是却取不到模板列里文本框和单选按钮的值,请高手帮忙看一下.前台页面代码:
<body>
    <form id="form1" runat="server">
        <uc1:LocationBar ID="Navigate" runat="server" />
        <table class="tar w97">
            <tr>
                <td align="left">
                    <asp:RadioButton ID="btnOne" runat="server" AutoPostBack="True" Checked="True" GroupName="Test"
                        Text="填报本条计划" OnCheckedChanged="btnOne_CheckedChanged" />
                    <asp:RadioButton ID="btnTwo" runat="server" AutoPostBack="True" GroupName="Test"
                        Text="填报所有计划" OnCheckedChanged="btnTwo_CheckedChanged" />
                </td>
                <td align="right">
                    <asp:Button ID="btnOk" CssClass="butt_bg" runat="server" Text=" 提  交 " OnClick="btnOk_Click" />&nbsp;
                    <asp:Button ID="btCancel" CssClass="butt_bg" CausesValidation="false" runat="server"
                        Text=" 取  消 " />
                </td>
            </tr>
        </table>
        <asp:DataList ID="dlDetail" runat="server" BorderStyle="None" BorderWidth="0px" RepeatLayout="table"
            GridLines="Horizontal">
            <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
            <EditItemStyle BackColor="Yellow"></EditItemStyle>
            <SelectedItemStyle Font-Bold="True"></SelectedItemStyle>
            <ItemTemplate>
                <table border="0" cellpadding="0" cellspacing="0" class="w97" style="margin-bottom: 0px;">
                    <tr>
                        <asp:HiddenField ID="hidId" Value='<%# DataBinder.Eval(Container.DataItem,"pdid") %>'
                            runat="server" />
                        <td width="120">
                            计划内容</td>
                        <td class="tal">
                            <%# DataBinder.Eval(Container.DataItem, "content")%>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            完成情况</td>
                        <td class="tal">
                            <asp:RadioButton ID="btnR1" runat="server" Checked="True" GroupName="R" Text="完成" />
                            <asp:RadioButton ID="btbR2" runat="server" GroupName="R" Text="未完成" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            备注说明</td>
                        <td class="tal">
                            <asp:TextBox ID="txtNote" TextMode="MultiLine" Rows="5" Width="300px" runat="server"></asp:TextBox></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </form>
</body>后台代码:protected void btnOk_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dlDetail.Items.Count; i++)
        {
            HiddenField hidId = dlDetail.Items[i].FindControl("hidId") as HiddenField;
            TextBox t = dlDetail.Items[i].FindControl("txtNote") as TextBox;
            RadioButton r1 = dlDetail.Items[i].FindControl("btnR1") as RadioButton;
            RadioButton r2 = dlDetail.Items[i].FindControl("btnR2") as RadioButton;            Response.Write("txtNode= "+ t.Text);
            Response.Write("r2= "+ r2.Checked);
        }
    }

解决方案 »

  1.   

    foreach (RepeaterItem di in ReProducts.Items)
            {
                if (((System.Web.UI.WebControls.CheckBox)(di.Controls[1])).Checked == true)
                {
                    comid = ((HtmlInputControl)di.Controls[3]).Value;
                }
            }
      

  2.   

    你们可能是直接读取初始化时绑定的数据了,所以能读出来.我的这个功能是页面加载完后,在list的模板列里选择单选按钮,填写文本框内容以后,你再点击提交按钮,这时候肯定读不出来数据,就算你读出来的数据,也是初始化时的数据.而不是后来手动填写的数据.
      

  3.   


    //其实也不用 HiddenField 也可以
    //这样
            DataTable dt= Get2005Pager("testtab", "*", "id", "", this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, 1,out sss);
            this.DataList1.DataSource = dt;
            this.DataList1.DataKeyField = "id";//数据库里主键字段
            this.DataList1.DataBind() 
    //获得
            for (int i = 0; i <this.DataList1.Items.Count; i++)
            {
                string keyid = this.DataList1.DataKeys[i].ToString();
                CheckBox t = (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
                if(t.Checked)
                {
                    Response.Write("txtNode= " + t.Text);
                  Response.Write("r2= " + r2.Checked);
                }
                
            } 
      

  4.   

    这是后台代码,前台只改了绑定字段的:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            dlDetail.DataSource = (new DAO()).GetTopNews();
            dlDetail.DataBind();
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            string str = "";
            for (int i = 0; i < dlDetail.Items.Count; i++)
            {
                TextBox txt = dlDetail.Items[i].FindControl("txtNote") as TextBox;
                str += txt.Text;
                RadioButton r1 = dlDetail.Items[i].FindControl("btnR1") as RadioButton;
                Response.Write("<script>alert('" + r1.Checked + "')</script>");
            }
            Response.Write("<script>alert('"+str+"')</script>");
        }
      

  5.   


     protected void btnOk_Click(object sender, EventArgs e)
        {
         string upsql = "update 表 set=123 where id=";//省略其他SET
            IList<string> l = new List<string>();
            for (int i = 0; i <this.DataList1.Items.Count; i++)
            {
                CheckBox c= (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
                TextBox tb = (TextBox)this.DataList1.Items[i].FindControl("TextBox1");
                //下面几个TextBox省略
                if(c.Checked)
                {
                    l.Add("update 表 set='"+tb.Text+"' where id="+ this.DataList1.DataKeys[i].ToString());
                }
            }
            SqlServerHelper.ExecuteSqlTran(l);
        }
    SqlServerHelper.cs类库
      /// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>
        public static void ExecuteSqlTran(IList<string> SQLStringList)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                SqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }
      

  6.   


    //完整的
    protected void btnOk_Click(object sender, EventArgs e)
        {
            IList<string> l = new List<string>();
            for (int i = 0; i <this.DataList1.Items.Count; i++)
            {
                CheckBox c= (CheckBox)this.DataList1.Items[i].FindControl("CheckBox1");
                TextBox tb = (TextBox)this.DataList1.Items[i].FindControl("TextBox1");
                //下面几个TextBox省略
                if(c.Checked)
                {
                    l.Add("update 表 set='"+tb.Text+"' where id="+ this.DataList1.DataKeys[i].ToString());
                }
            }
            SqlServerHelper.ExecuteSqlTran(l);
        }
    SqlServerHelper.cs//数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.
        public static string connectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        /// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>
        public static void ExecuteSqlTran(IList<string> SQLStringList)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                SqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }