1

我正在尝试从动态创建的服务器文本框中获取文本。我只是通过将文本放入我放置在 aspx 文件中的另一个文本框中进行检查,但我得到的始终是一个空字符串..(我还尝试了另一种包含在备注中的方法,但它也不起作用)

这是后面的代码:

public partial class Product_list : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        create_table();
    }

    protected void create_table()
    {
        DBServices db1=new DBServices();
        List<Product> list1 = db1.ReadProducts();
        int id_number=1;
        int id_numer2 = 7;
        int id_number3 = 13;

        Table tbl = new Table();
        tbl.ID = "tbl1";
        this.Controls.Add(tbl);

        foreach (Product p1 in list1)
        {
            TableRow rw = new TableRow();
            rw.ID = Convert.ToString(id_numer2);

            TableCell cell1 = new TableCell();
            cell1.Text = p1.Name;

            TableCell cell2 = new TableCell();

            Image img = new Image();
            img.ImageUrl = p1.ImagePath;
            img.Height = 50;
            img.Width = 50;

            cell2.Controls.Add(img);

            TableCell cell3 = new TableCell();
            cell3.ID = Convert.ToString(id_number3);

            TextBox textbox1 = new TextBox();
            textbox1.ID = Convert.ToString(id_number);

            cell3.Controls.Add(textbox1);

            rw.Controls.Add(cell1);
            rw.Controls.Add(cell2);
            rw.Controls.Add(cell3);

            tbl.Controls.Add(rw);

            id_number++;
            id_numer2++;
            id_number3++;
        } 
    }

    void save_list()
    {
        List<Product> Items_list = new List<Product>();

        //TextBox aControl =Page.FindControl("1") as TextBox;

        /var tbl1 = this.Page.FindControl("tbl1") as Table;
        var tr = tbl1.FindControl("7") as TableRow;
        var td = tr.FindControl("13") as TableCell;
        var txt = td.FindControl("1") as TextBox;

        txt1.Value = txt.Text;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        save_list();
    }
}

这是aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Product_list.aspx.cs" Inherits="Product_list" %>
<!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></title>
<script runat=server>
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="header">
            <h1>Items List</h1>
        </div>

        <div id="prod_table" runat="server"></div>
        <div>
            <input type="text" runat="server" id="txt1" />
            <asp:Button ID="Button1" runat="server" Text="Button"
                onclick="Button1_Click" />
        </div>
    </form>
</body>
</html>
4

1 回答 1

0

您应该了解,您在服务器的每个帖子上都重新创建了您的表。因此,您不能依赖动态表中的数据,因为每次发布后它都会丢失。我的建议是将此值保留在具有“隐藏”类型的永久输入控件中。然后在按钮单击事件上获取此值并将其分配给文本框。

于 2012-12-09T19:53:30.310 回答