2011-04-01 53 views
0

請查找我的aspx和codebind文件,我不明白爲什麼我無法在代碼部署後在我的頁面上看到文本框。 ASCX代碼無法看到動態加載的文本框

<asp:DropDownList ID="DropDownList1" runat="server" 
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" > 

<asp:ListItem Text="one" Enabled="true" Value="1" Selected="True"></asp:ListItem> 
<asp:ListItem Text="two" Enabled="true" Value="2" ></asp:ListItem> 

</asp:DropDownList> 

我想獲得動態加載在下拉框的變化的文本框,並獲得在文本框到結果textbox.Once我改變下拉框中輸入的值,我得到的texboxes,但當我點擊按鈕,文本框消失...我需要使用視圖狀態,但在哪裏以及如何使用它來獲得此代碼工作請幫助我

請找到我的codeattached。

public partial class DropdowndynamicUserControl : UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    protected void createtextboxes(int i) 
    { 
     DynPanel.Visible = true; 
     for (int counter = 0; counter <= i; counter++) 
     { 
      TextBox tb = new TextBox(); 
      tb.Width = 150; 
      tb.Height = 18; 
      tb.TextMode = TextBoxMode.SingleLine; 
      tb.ID = "TextBoxID" + (counter + 1).ToString(); 
      tb.Text = "EnterTitle" + counter; 
      tb.Visible = true; 
      tb.EnableViewState = true; 
      DynPanel.Controls.Add(tb); 
      DynPanel.Controls.Add(new LiteralControl("<br/>")); 

     } 


    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int i; 

     string selection = DropDownList1.SelectedItem.Value.ToString(); 

     if (selection == "1") 
     { 
      i = 1; 
      createtextboxes(i); 
     } 
     else if (selection == "2") 
     { 
      i = 2; 
      createtextboxes(i); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     readtextboxes(); 

    } 

    public void readtextboxes() 
    { 
     string x = string.Empty; 
     for (int a = 0; a < DynPanel.Controls.Count; a++) 
     { 
      foreach (Control ctrl in DynPanel.Controls) 
      { 
       if (ctrl is TextBox) 
       { 
        x = ((TextBox)ctrl).Text; 
       } 
      } 
     } 
    } 


} 

回答

2

你將你所有的TextBox控件的一個面板,其屬性設置爲Visible="false"。如果面板不可見,則不會呈現任何控件。

+0

哎呀,謝謝你糾正這種... :) – Janet 2011-04-01 19:18:25

0

你缺少base.CreateChildControls();

protected override void CreateChildControls()