2014-01-21 34 views
0

我有1個文本框和1個按鈕。我想要顯示5 div,如果用戶在文本框中輸入5並單擊按鈕例如。顯示div而不是TextBox c#

我有這個,但我不知道如何使它到div的

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int NumberOfTextBoxes = Convert.ToInt32(TextBox_UserEntry.Text); 
    for (int i = 0; i < NumberOfTextBoxes; i++) 
    { 
     TextBox tb = new TextBox(); 
     tb.ID = "tb" + Convert.ToInt32(i + 1); 
     Panel1.Controls.Add(tb); 
    } 
} 

你可以幫我改

TextBox tb = new TextBox(); 
tb.ID = "tb" + Convert.ToInt32(i + 1); 
Panel1.Controls.Add(tb); 

div

   <asp:TextBox ID="TextBox_UserEntry" runat="server"></asp:TextBox> 
       <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
       <asp:Panel ID="ParentPanel" runat="server"> 
       </asp:Panel> 

DIV

   <div id="divOccupantProfile"> 

       <asp:Label ID="OPfamilyname" runat="server" Text="Family Name"></asp:Label> 
       <asp:TextBox ID="textOPfamilyname" runat="server"></asp:TextBox><br /> 

       <asp:Label ID="OPfirstname" runat="server" Text="First Name"></asp:Label> 
       <asp:TextBox ID="textOPfirstname" runat="server"></asp:TextBox><br /> 

       <asp:Label ID="OPmiddlename" runat="server" Text="Middle Name"></asp:Label> 
       <asp:TextBox ID="textOPmiddlename" runat="server"></asp:TextBox><br /> 

       <asp:Label ID="OPmaritalstatus" runat="server" Text="Marital Status"></asp:Label> 
       <asp:DropDownList ID="ddlOPmaritalstatus" runat="server" > 
        <asp:ListItem></asp:ListItem> 
        <asp:ListItem>Married</asp:ListItem> 
        <asp:ListItem>Single</asp:ListItem> 
        <asp:ListItem>Divorced</asp:ListItem> 
       </asp:DropDownList><br /> 

       <asp:Label ID="OPoccupation" runat="server" Text="Occupation"></asp:Label> 
       <asp:TextBox ID="textOPoccupation" runat="server"></asp:TextBox><br /> 

       <asp:Label ID="OPrelationship" runat="server" Text="Relationship"></asp:Label> 
       <asp:DropDownList ID="ddlOPrelationship" runat="server" > 
        <asp:ListItem></asp:ListItem> 
        <asp:ListItem>Wife</asp:ListItem> 
        <asp:ListItem>Daughter</asp:ListItem> 
        <asp:ListItem>Son</asp:ListItem> 
        <asp:ListItem>Father</asp:ListItem> 
        <asp:ListItem>Mother</asp:ListItem> 
        <asp:ListItem>House helper</asp:ListItem> 
        <asp:ListItem>Driver</asp:ListItem> 
       </asp:DropDownList> 

       </div> 
+0

我希望是ASP.NET WebForms? –

+0

是@SergeyBerezovskiy –

回答

1

您可以使用Panel來實現此目的。它呈現爲Div

int NumberOfDiv = Convert.ToInt32(TextBox_UserEntry.Text); 
for (int i = 0; i < NumberOfDiv; i++) 
{ 
    Panel pnl = new Panel(); 
    pnl.ID = "pnl" + Convert.ToInt32(i + 1); 
    ParentPanel.Controls.Add(pnl); 
} 
+0

什麼是'ParentPanel'是一個ID我得到一個錯誤 –

+0

重命名您的'Panel1'爲'ParentPanel'或用'Panel1'代替它 – Arshad

+0

哦對不起,我忘了我沒有把我的asp代碼 –

相關問題