访问TextBox ID

时间:2013-07-11 19:25:43

标签: c# asp.net textbox

每次按点击时,我都会创建文本框的方法。不过,我尝试在其他方法中访问Textbox ID 。似乎有些东西超出范围,这是代码:

    public partial class _Default : System.Web.UI.Page
{
    TextBox box = new TextBox();

    protected void Page_Load(object sender, EventArgs e)
    {



    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
        Label1.Text = ViewState["count"].ToString();
        int test = int.Parse(string.Format("{0}", ViewState["count"]));

            for (int j = 0; j < test; j++)
            {

                TableRow r = new TableRow();

                box.ID = "Textbox" + j;

                TableCell c = new TableCell();
                c.Controls.Add(box);
                r.Cells.Add(c);
                table1.Rows.Add(r);
                Response.Write(box.ID);

            }

            if (test == 4)
            {
                Button1.Visible = false;
            }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        Response.Write(box.ID);
    }

我希望Button2_Click打印box.ID以确保它能够访问它。

4 个答案:

答案 0 :(得分:3)

您的Button1_ClickButton2_Click事件将在两个单独的回发中进行。 Box除了在此方案中的默认构造函数中设置的任何值之外,不会为其分配任何值。

编辑:因为您在回发时丢失了所有代码隐藏对象和值的状态。

答案 1 :(得分:2)

要始终创建与用户点击Button1的次数相同的文本框,在Page_Load中您必须运行代码:

int test = 0;
if (ViewState["count"] != null)
   test = (int)ViewState["count"];

for (int j = 0; j < test; j++)
{
    TextBox box = new TextBox();
    box.ID = "Textbox" + j;
    textBoxes.Add(box.ID, box);
    TableRow r = new TableRow();
    TableCell c = new TableCell();
    c.Controls.Add(box);
    r.Cells.Add(c);
    table1.Rows.Add(r);
}

其中textBoxesPage类成员:

Dictionary<string, TextBox> textBoxes = new Dictionary<string, TextBox>();

然后,在Button2_Click

string ids = "";
foreach(string id in textBoxes.Keys)
{
    ids = ids + id + ",";
}
Response.Write(ids);

答案 2 :(得分:2)

关键是你需要在回发时重新加载动态创建的控件。

否则,它们将变为null,并且您无法访问这些值。

注意: 您无需使用Response.Write

以下是示例。

<asp:Label runat="server" ID="Label1"></asp:Label>
<asp:Table runat="server" ID="table1"></asp:Table>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" 
   Text="Create TextBoxes" />
<asp:Button runat="server" ID="Button2" OnClick="Button2_Click" 
  Text="Submit" />

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        // Reload those control back. It can be in either init or load event.
        int total = Count;
        Label1.Text = total.ToString();

        for (int i = 0; i < total; i++)
            CreateTextBoxes(i);

        Count = total;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    Count = Count++;
    CreateTextBoxes(Count);

    if (Count == 4)
    {
        Button1.Visible = false;
    }
}

protected void Button2_Click(object sender, EventArgs e)
{
    var results = new List<string>();
    foreach (TableRow row in table1.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            var textBox = cell.Controls[0] as TextBox;
            if (textBox != null)
            {
                results.Add(textBox.Text);
            }
        }
    }
    Label1.Text += string.Join(",", results);
}

private int Count
{
    get { return Convert.ToInt32(ViewState["count"] ?? "0"); }
    set { ViewState["count"] = value; }
}

private void CreateTextBox(int j)
{
    var box = new TextBox();
    box.ID = "Textbox" + j;
    box.Text = "Textbox" + j;

    var c = new TableCell();
    c.Controls.Add(box);

    var r = new TableRow();
    r.Cells.Add(c);

    table1.Rows.Add(r);
}

输出

enter image description here

答案 3 :(得分:0)

通过 asp.net 的方式,您可以在每次回发时重新创建box(当您点击Button1和点击Button2时) 。因此,当您点击Button2时,即使您为Button1运行了代码,您也会获得一个全新的box.

Button2中,您需要执行一些操作来从您添加它的页面本身访问textbox。我不赞成如何处理你正在做的事情,但是像

那样
var mytextBox = table1.Rows[x].Cells[y].Controls[z] as TextBox; // PSEUDOCODE