我的html上有一个asp:占位符,我正在访问它来添加文本框和标签来回答问题。
“表单”是正确创建的--我得到了所有带有问题的标签和每个答案的文本框,但是当我尝试单击runatserver按钮后,Placeholder1总是空的。
我已经尝试了大量的东西,以获得文本框值插入到数据库中没有运气。
在我的代码下面。
for表单中表单的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#代码可以得到多少个问题,然后为每个答案添加一个文本框,使用带计数器的圆锥键添加一个id+counter
/* c# a counter is made to increment a number to the id*/
Adding controls to the PlaceHolder1
/* add controls: */在计数器内添加all结果*/的所有行数的标签
标签
Label quest= new Label();
quest.ID = "quest" + counter;
quest.Attributes.Remove("class");
quest.Attributes.Add("class", "exampleFormControlInput1");
quest.text = "sql query";Textbox /add文本框在圆锥键上/
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>"));检查PlaceHolder1中有多少控件
试图检查PlaceHolder1中有多少控件
count = PlaceHolder1.Controls.Count;总是0
发布于 2019-02-06 19:33:40
您可能没有在PostBack上重新创建控件。请参阅此工作示例。
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);
}
}然后在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>";
}
}https://stackoverflow.com/questions/54561071
复制相似问题