我在動態添加文本框,當我點擊一個提交按鈕並有回傳時,我看不到輸入到文本框中的值,所有這些都會彈出來。這裏的.aspx頁面中... \在按鈕上點擊動態添加文本框
form id="form1" runat="server">
<asp:PlaceHolder ID="phFormContent" runat="server">
</asp:PlaceHolder>
<br /><br />
<asp:Button ID="btnAddForm" runat="server" Text="Add Form" OnClick="btnAddForm_Click" />
<asp:Button ID="btnSubmitForms" runat="server" Text="Submit Forms" OnClick="btnSubmit_Click" />
</form>
...這裏就是我如何添加文本框窗體上單擊btnAddForm ...
protected void btnAddForm_Click(object sender, EventArgs e)
{
// Create Labels
Label lblName = new Label();
lblName.Text = "NAME:";
Label lblNumber = new Label();
lblNumber.Text = "NUMBER:";
Label lblAddress = new Label();
lblAddress.Text = "ADDRESS:";
Label lblCompany = new Label();
lblCompany.Text = "COMPANY:";
// Create Text Boxes
TextBox txtName = new TextBox();
TextBox txtNumber = new TextBox();
TextBox txtAddress = new TextBox();
TextBox txtCompany = new TextBox();
// Create submit button
Button btnSubmit = new Button();
btnSubmit.Text = "SUBMIT";
// Create panel and add controls
Panel pnlForm = new Panel();
pnlForm.Controls.Add(lblName);
pnlForm.Controls.Add(txtName);
pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
pnlForm.Controls.Add(lblNumber);
pnlForm.Controls.Add(txtNumber);
pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
pnlForm.Controls.Add(lblAddress);
pnlForm.Controls.Add(txtAddress);
pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
pnlForm.Controls.Add(lblCompany);
pnlForm.Controls.Add(txtCompany);
pnlForm.Controls.Add(new LiteralControl("<hr />"));
pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
panels.Add(pnlForm);
foreach (Control panel in panels)
{
phFormContent.Controls.Add(panel);
}
}
...這是我如何努力提取字段加入每個單獨面板...
private static void GetFormFields(Control panelControl)
{
ControlCollection controls = panelControl.Controls;
foreach (Control childControl in panelControl.Controls)
{
if (childControl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
TextBox txt = childControl as TextBox;
fields.Add(txt);
}
else
{
GetFormFields(childControl);
}
}
}
面板和字段是靜態列表,每個都包含四個字段面板。我通過GetFormFields個別面板的參考...
private static List<Control> panels = new List<Control>();
private static List<TextBox> fields = new List<TextBox>();
您是否能夠看到您添加的文本框,並且這些值只是空白,或者您根本無法看到動態添加的文本框? – Cortright
我能夠看到文本框就好,只是它們是Text屬性是一個空字符串,而頁面在相應的文本輸入中顯然具有文本。 – MassStrike
您的頁面是否啓用了ViewState?添加它們時,您可以嘗試手動設置控件ViewstateMode。像這樣:'txtName.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;' – Cortright