#region //尺寸选择DimensionChanged
    protected void ddl_item_dimension_SelectedIndexChanged(object sender, EventArgs e)
    {
        Function fc = new Function();
        if (fc.IsNumberic(ddl_item_dimension.SelectedValue.ToString()))
        {
            int dtype = Convert.ToInt32(this.ddl_item_dimension.SelectedValue);
            TableRow tRow1 = new TableRow();
            TableCell tCell1 = new TableCell();
            TableCell tCell2 = new TableCell();
            Table1.Rows.Add(tRow1);
            tRow1.BackColor = Color.LightGray;
            tCell1.Controls.Add(new LiteralControl("NO."));
            tCell1.HorizontalAlign = HorizontalAlign.Center;
            tCell1.BackColor = Color.LightGray;
            tRow1.Cells.Add(tCell1);            tCell2.Controls.Add(new LiteralControl("Multi-Dimensions"));
            tCell2.HorizontalAlign = HorizontalAlign.Center;
            tRow1.Cells.Add(tCell2);
            for (int i = 0; i < dtype; i++)
            {
                TableRow tRow = new TableRow();
                TableCell tCell = new TableCell();                Table1.Rows.Add(tRow);
                tCell.Controls.Add(new LiteralControl((i+1)+"."));
                tCell.HorizontalAlign = HorizontalAlign.Center;
                if (i % 2 == 0)
                {
                    tCell.BackColor = Color.WhiteSmoke;
                }
                else
                {
                    tCell.BackColor = Color.LightGray;
                }
                tRow.Cells.Add(tCell);
                //
                TableCell tCellT = new TableCell();
                //                tCellT.HorizontalAlign = HorizontalAlign.Center;
                tCellT.Width = Unit.Parse("90%");
                if (i % 2 == 0)
                {
                    tCellT.BackColor = Color.WhiteSmoke;
                }
                else
                {
                    tCellT.BackColor = Color.LightGray;
                }
                tCellT.Controls.Add(new LiteralControl("L:"));
                System.Web.UI.WebControls.TextBox tBoxL = new TextBox();
                tBoxL.Width = Unit.Parse("30px");
                tBoxL.ID = "L" + i;
                tBoxL.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                tCellT.Controls.Add(tBoxL);
                tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                tCellT.Controls.Add(new LiteralControl("W:"));
                System.Web.UI.WebControls.TextBox tBoxW = new TextBox();
                tBoxW.Width = Unit.Parse("30px");
                tBoxW.ID = "W" + i;
                tBoxW.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                tCellT.Controls.Add(tBoxW);
                tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                tCellT.Controls.Add(new LiteralControl("H:"));
                System.Web.UI.WebControls.TextBox tBoxH = new TextBox();
                tBoxH.Width = Unit.Parse("30px");
                tBoxH.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                tBoxH.ID = "H" + i;
                tCellT.Controls.Add(tBoxH);
                tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                tCellT.Controls.Add(new LiteralControl("X:"));
                System.Web.UI.WebControls.TextBox tBoxX = new TextBox();
                tBoxX.Width = Unit.Parse("30px");
                tBoxX.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                tBoxX.ID = "X" + i;
                tCellT.Controls.Add(tBoxX);
                tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                //
                tRow.Cells.Add(tCellT);                //
            }        }
        else { }
    }
    #endregion我这样动态生成一个表格,表格里放了textbox,我怎样在提交的时候得到这些textbox的值呢.??

解决方案 »

  1.   

    (TextBox)Table1.Rows[0].Cells[1].FindControl("TextBox控件的ID").Text
      

  2.   

    再定义个hidden,在定义textbox时定义个onchange()事件,把textbox的内容放到hidden里,保存的时候按hidden的内容保存好了
      

  3.   

    string strName="";
    TextBox tb=null;
    string strValue="";
    for (int i = 0; i < dtype; i++)
    {
      strName="L"+i.ToString();
      tb=Page.FindControl(strName) as TextBox;
      strValue+=tb.Text+",";
    }
      

  4.   

    不行啊,生成表格之后 this.Table1.Rows.Count 怎么是零啊.
      

  5.   

    代码贴出来,帮我看看看啊
    default2.aspx:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:DropDownList ID="ddl_item_dimension" runat="server" ValidationGroup="shipment" AutoPostBack="True" OnSelectedIndexChanged="ddl_item_dimension_SelectedIndexChanged">
                    <asp:ListItem Selected="True" Value="0">Dimension Type</asp:ListItem>
                    <asp:ListItem Value="1">1 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="2">2 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="3">3 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="4">4 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="5">5 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="6">6 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="7">7 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="8">8 Type of Packing</asp:ListItem>
                    <asp:ListItem Value="9">9 Type of Packing</asp:ListItem>
                </asp:DropDownList>
                <asp:Table ID="Table1" runat=server></asp:Table>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
        </form>
    </body>
    </html>default2.aspx.cs:using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        #region //尺寸选择DimensionChanged
        protected void ddl_item_dimension_SelectedIndexChanged(object sender, EventArgs e)
        {
            Function fc = new Function();
            if (fc.IsNumberic(ddl_item_dimension.SelectedValue.ToString()))
            {
                int dtype = Convert.ToInt32(this.ddl_item_dimension.SelectedValue);
                TableRow tRow1 = new TableRow();
                TableCell tCell1 = new TableCell();
                TableCell tCell2 = new TableCell();
                Table1.Rows.Add(tRow1);
                tRow1.BackColor = Color.LightGray;
                tCell1.Controls.Add(new LiteralControl("NO."));
                tCell1.HorizontalAlign = HorizontalAlign.Center;
                tCell1.BackColor = Color.LightGray;
                tRow1.Cells.Add(tCell1);            tCell2.Controls.Add(new LiteralControl("Multi-Dimensions"));
                tCell2.HorizontalAlign = HorizontalAlign.Center;
                tRow1.Cells.Add(tCell2);
                for (int i = 0; i < dtype; i++)
                {
                    TableRow tRow = new TableRow();
                    TableCell tCell = new TableCell();                Table1.Rows.Add(tRow);
                    tCell.Controls.Add(new LiteralControl((i + 1) + "."));
                    tCell.HorizontalAlign = HorizontalAlign.Center;
                    if (i % 2 == 0)
                    {
                        tCell.BackColor = Color.WhiteSmoke;
                    }
                    else
                    {
                        tCell.BackColor = Color.LightGray;
                    }
                    tRow.Cells.Add(tCell);
                    //
                    TableCell tCellT = new TableCell();
                    //                tCellT.HorizontalAlign = HorizontalAlign.Center;
                    tCellT.Width = Unit.Parse("90%");
                    if (i % 2 == 0)
                    {
                        tCellT.BackColor = Color.WhiteSmoke;
                    }
                    else
                    {
                        tCellT.BackColor = Color.LightGray;
                    }
                    tCellT.Controls.Add(new LiteralControl("L:"));
                    System.Web.UI.WebControls.TextBox tBoxL = new TextBox();
                    tBoxL.Width = Unit.Parse("30px");
                    tBoxL.ID = "L" + i;
                    tBoxL.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                    tCellT.Controls.Add(tBoxL);
                    tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                    tCellT.Controls.Add(new LiteralControl("W:"));
                    System.Web.UI.WebControls.TextBox tBoxW = new TextBox();
                    tBoxW.Width = Unit.Parse("30px");
                    tBoxW.ID = "W" + i;
                    tBoxW.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                    tCellT.Controls.Add(tBoxW);
                    tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                tCellT.Controls.Add(new LiteralControl("H:"));
                    System.Web.UI.WebControls.TextBox tBoxH = new TextBox();
                    tBoxH.Width = Unit.Parse("30px");
                    tBoxH.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                    tBoxH.ID = "H" + i;
                    tCellT.Controls.Add(tBoxH);
                    tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                tCellT.Controls.Add(new LiteralControl("X:"));
                    System.Web.UI.WebControls.TextBox tBoxX = new TextBox();
                    tBoxX.Width = Unit.Parse("30px");
                    tBoxX.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                    tBoxX.ID = "X" + i;
                    tCellT.Controls.Add(tBoxX);
                    tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                    //
                    tRow.Cells.Add(tCellT);                //
                }        }
            else { }
        }
        #endregion    protected void Button1_Click(object sender, EventArgs e)
        {
            string strName = "";
            TextBox tb = null;
            string strValue = "";
            for (int i = 0; i < Convert.ToInt32(this.ddl_item_dimension.SelectedItem.Value.ToString()); i++)
            {
                strName = "L" + i.ToString();
                //tb = (TextBox)Table1.Rows[i+1].Cells[i+1].FindControl(strName);
                tb = Page.FindControl(strName) as TextBox;
                strValue += tb.Text + ",";
            }        Response.Write(strValue);
        }
    }
      

  6.   

    问题分析:由于Table1是动态生成的,在载入页面时,Table1仍然没有任何行列,故页面的ViewState中并没有table1的行列信息,因此在Button1_Click事件中是找不到行列的改成如下:注意Cells[i+1]有错误,应该是Cells[1],你的表格只有2列,TextBox在第2列using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (ddl_item_dimension.SelectedValue.ToString() != "")
                {
                    int dtype = Convert.ToInt32(this.ddl_item_dimension.SelectedValue);
                    TableRow tRow1 = new TableRow();
                    TableCell tCell1 = new TableCell();
                    TableCell tCell2 = new TableCell();
                    Table1.Rows.Add(tRow1);
                    tRow1.BackColor = Color.LightGray;
                    tCell1.Controls.Add(new LiteralControl("NO."));
                    tCell1.HorizontalAlign = HorizontalAlign.Center;
                    tCell1.BackColor = Color.LightGray;
                    tRow1.Cells.Add(tCell1);                tCell2.Controls.Add(new LiteralControl("Multi-Dimensions"));
                    tCell2.HorizontalAlign = HorizontalAlign.Center;
                    tRow1.Cells.Add(tCell2);
                    for (int i = 0; i < dtype; i++)
                    {
                        TableRow tRow = new TableRow();
                        TableCell tCell = new TableCell();                    Table1.Rows.Add(tRow);
                        tCell.Controls.Add(new LiteralControl((i + 1) + "."));
                        tCell.HorizontalAlign = HorizontalAlign.Center;
                        if (i % 2 == 0)
                        {
                            tCell.BackColor = Color.WhiteSmoke;
                        }
                        else
                        {
                            tCell.BackColor = Color.LightGray;
                        }
                        tRow.Cells.Add(tCell);
                        //
                        TableCell tCellT = new TableCell();
                        //                    tCellT.HorizontalAlign = HorizontalAlign.Center;
                        tCellT.Width = Unit.Parse("90%");
                        if (i % 2 == 0)
                        {
                            tCellT.BackColor = Color.WhiteSmoke;
                        }
                        else
                        {
                            tCellT.BackColor = Color.LightGray;
                        }
                        tCellT.Controls.Add(new LiteralControl("L:"));
                        System.Web.UI.WebControls.TextBox tBoxL = new TextBox();
                        tBoxL.Width = Unit.Parse("30px");
                        tBoxL.ID = "L" + i;
                        tBoxL.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                        tCellT.Controls.Add(tBoxL);
                        tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                        tCellT.Controls.Add(new LiteralControl("W:"));
                        System.Web.UI.WebControls.TextBox tBoxW = new TextBox();
                        tBoxW.Width = Unit.Parse("30px");
                        tBoxW.ID = "W" + i;
                        tBoxW.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                        tCellT.Controls.Add(tBoxW);
                        tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                    tCellT.Controls.Add(new LiteralControl("H:"));
                        System.Web.UI.WebControls.TextBox tBoxH = new TextBox();
                        tBoxH.Width = Unit.Parse("30px");
                        tBoxH.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                        tBoxH.ID = "H" + i;
                        tCellT.Controls.Add(tBoxH);
                        tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));                    tCellT.Controls.Add(new LiteralControl("X:"));
                        System.Web.UI.WebControls.TextBox tBoxX = new TextBox();
                        tBoxX.Width = Unit.Parse("30px");
                        tBoxX.ToolTip = "International Volume weight =Inch: L*W*H/166 (LB)   CM:  L*W*H/6000(Kg) ,Domestic Volume weight =Inch: L*W*H/194 (LB)";
                        tBoxX.ID = "X" + i;
                        tCellT.Controls.Add(tBoxX);
                        tCellT.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));
                        //
                        tRow.Cells.Add(tCellT);                    //
                    }
                }
                else { }
            }    }
        #region //尺寸选择DimensionChanged
        protected void ddl_item_dimension_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Function fc = new Function();    }
        #endregion    protected void Button1_Click(object sender, EventArgs e)
        {
            string strName = "";
            TextBox tb = null;
            string strValue = "";
            for (int i = 0; i < Convert.ToInt32(this.ddl_item_dimension.SelectedItem.Value.ToString()); i++)
            {
                strName = "L" + i.ToString();
                tb = (TextBox)Table1.Rows[i+1].Cells[1].FindControl(strName);
                //tb = Page.FindControl(strName) as TextBox;
                strValue += tb.Text + ",";
            }        Response.Write(strValue);
        }
    }=====================================================
    The CodeGear.cn 你身边的开发资源
      

  7.   

    用TextBox[] 遍历数组就能得到值了
      

  8.   

    很简单啊,用Reqeust就行了: private void Button1_Click(object sender, System.EventArgs e)
    {
    string strName = "";
    TextBox tb = null;
    string strValue = "";
    for (int i = 0; i < Convert.ToInt32(this.ddl_item_dimension.SelectedItem.Value.ToString()); i++)
    {
    strName = "L" + i.ToString();
    //tb = (TextBox)Table1.Rows[i+1].Cells[i+1].FindControl(strName);
    //tb = Page.FindControl(strName) as TextBox;
    //strValue += tb.Text + ",";
    strValue += Request[strName].ToString();
    } Response.Write(strValue);
      

  9.   

    同意楼上的,用Request可以取得到.