简体   繁体   English

如何从占位符获取所有文本框? asp.net c#网络表格

[英]How to get all textbox from a placeholder? asp.net c# webform

I have a asp:placeholder on my html and I'm accessing to it to add textboxes and labels for questions and answers. 我的html上有一个asp:placeholder,我正在访问它以添加文本框和标签来回答问题。

The "form" is created correctly I got all the labels with the questions and a textbox for each answer, however when I try after the button click runatserver the Placeholder1 is always empty. 正确创建了“表单”,我得到了带有问题的所有标签以及每个答案的文本框,但是当我单击按钮后,单击runatserver时,Placeholder1始终为空。

I have tried a lof of things to get the textbox values to insert on a database without luck. 我尝试了很多事情来获取文本框值,以便在没有运气的情况下插入数据库。

Below my code. 在我的代码下面。

Thank you for your help in advance. 提前谢谢你的帮助。

HTML code for the form inside a webform: Web表单中表单的HTML代码:

/* form for the buttons and title*/

<form id="form2" runat="server">

    <div align="center">
    </div>
    <div>
        <div align="center" class="form-group">
            <h4>
                <asp:label runat="server" id="title"></asp:label>
            </h4>
            <br />
            <br />
            <div align="center">
                place holder for the questions&answers

                    <asp:placeholder id="Placeholder1" runat="server">
                    </asp:placeholder>
            </div>
        </div>
    </div>
    <br />
    <br />

C# code to get how many questions and then add a textbox for each answer, using the cicle with a counter to add a id+counter C#代码获取多少个问题,然后为每个答案添加一个文本框,使用带有计数器的cicle来添加id + counter

/* c# a counter is made to increment a number to the id*/

Adding controls to the PlaceHolder1     
/* add controls: */

add labels inside the counter for all number of rows of the sqlresult*/ 为sqlresult的所有行数在计数器内添加标签* /

Labels 标签

Label quest= new Label();
quest.ID = "quest" + counter;
quest.Attributes.Remove("class");
quest.Attributes.Add("class", "exampleFormControlInput1");

quest.text = "sql query";

Textbox / add textbox on the cicle / 文本框/ 在提示框上添加文本框 /

TextBox answer = new TextBox();
answer .ID = "answer " + counter;
answer .Attributes.Remove("class");
answer .Attributes.Add("class", "form-control");

PlaceHolder1.Controls.Add(quest);
PlaceHolder1.Controls.Add(new LiteralControl("<br>"));
PlaceHolder1.Controls.Add(answer);
PlaceHolder1.Controls.Add(new LiteralControl("<br>"));

Check how many controls are inside of the PlaceHolder1 检查PlaceHolder1内部有多少个控件

Trying to check how many controls in the PlaceHolder1 尝试检查PlaceHolder1中有多少个控件

count = PlaceHolder1.Controls.Count;

always 0 总是0

You probably are not recreating the Controls on PostBack. 您可能没有在PostBack上重新创建控件。 See this working example. 请参阅此工作示例。

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        //do not create controls here
    }

    for (int count = 0; count < 5; count++)
    {
        TextBox answer = new TextBox();
        answer.ID = "answer " + count;
        PlaceHolder1.Controls.Add(answer);
    }
}

And then on PostBack 然后在回发

protected void Button1_Click(object sender, EventArgs e)
{
    int count = PlaceHolder1.Controls.Count;

    for (int i = 0; i < count; i++)
    {
        TextBox answer = PlaceHolder1.FindControl("answer " + i) as TextBox;
        Label1.Text += answer.Text + "<br>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM