簡體   English   中英

如何從占位符獲取所有文本框? asp.net c#網絡表格

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

我的html上有一個asp:placeholder,我正在訪問它以添加文本框和標簽來回答問題。

正確創建了“表單”,我得到了帶有問題的所有標簽以及每個答案的文本框,但是當我單擊按鈕后,單擊runatserver時,Placeholder1始終為空。

我嘗試了很多事情來獲取文本框值,以便在沒有運氣的情況下插入數據庫。

在我的代碼下面。

提前謝謝你的幫助。

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#代碼獲取多少個問題,然后為每個答案添加一個文本框,使用帶有計數器的cicle來添加id + counter

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

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

為sqlresult的所有行數在計數器內添加標簽* /

標簽

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

quest.text = "sql query";

文本框/ 在提示框上添加文本框 /

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

您可能沒有在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);
    }
}

然后在回發

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