如何在asp.net中创建动态文本框,下拉列表

时间:2013-08-21 10:14:30

标签: asp.net

我想在按钮点击的每个文本框和下拉列表下创建动态2文本框和2下拉列表。这是我正在使用的代码,它创建动态文本框和下拉列表,但它以水平方式创建控件。我希望以垂直方式创建动态控件,这将在文本框和下拉列表中进行。

<div id="dvContainer" runat="server">

<table align="center" cellpadding="0" cellspacing="0" width="90%">
<tr>
<td>
    <table width="100%">
        <tr>
            <td align="center">Date</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" Width="150px">                </asp:TextBox>
            </td>
            <td align="center">Time</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server" Width="150px"></asp:TextBox>
            </td>
            <td align="center">Godown</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
                </asp:DropDownList>
            </td>
        </tr>
    </table>
</td>
</tr>

<tr>
<td><table width="100%">
        <tr>
            <td align="center">Item</td>
            <td align="center">QTY</td>
            <td align="center">Unit</td>
            <td align="center">Available</td>
            <td align="center"></td>

        </tr>
        <tr>
            <td align="center">
                <asp:DropDownList ID="DropDownList2" runat="server" Width="150px">
                </asp:DropDownList>
            </td>
            <td align="center">
                <asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
            </td>
            <td align="center">
                <asp:DropDownList ID="DropDownList3" runat="server" Width="150px">
                </asp:DropDownList>
            </td>
            <td align="center">
                <asp:TextBox ID="TextBox4" runat="server" Width="150px"></asp:TextBox>
            </td>
             <td align="center">
                 <asp:Button ID="Button1" runat="server" Text="Button"   onclick="Button1_Click" />
            </td>

        </tr>

    </table></td>
</tr>

</table>

</div>

这是我在cs页面中使用的代码

public partial class StockEntry : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_Item;
protected System.Web.UI.WebControls.TextBox Txt_Quantity;
protected System.Web.UI.WebControls.DropDownList Ddl_Unit;
protected System.Web.UI.WebControls.TextBox Txt_Available;

protected void Page_Load(object sender, EventArgs e)
{

}

int countTimes = 0;

protected void Button1_Click(object sender, EventArgs e)
{
    if (ViewState["countTimes"] == null)
    {
        countTimes = 1;
    }
    else
    {
        countTimes = Convert.ToInt32(ViewState["countTimes"]);
    }

    for (int i = 0; i < countTimes; i++)
    {
        Ddl_Item = new DropDownList();
        Ddl_Item.ID = "Ddl_Item" + i;
        Ddl_Item.Width = 180 + i;

        Txt_Quantity = new TextBox();
        Txt_Quantity.ID = "Txt_Quantity" + i;
        Txt_Quantity.Width = 180 + i;

        Ddl_Unit = new DropDownList();
        Ddl_Unit.ID = "Ddl_Unit" + i;
        Ddl_Unit.Width = 180 + i;

        Txt_Available = new TextBox();
        Txt_Available.ID = "Txt_Available" + i;
        Txt_Available.Width = 180 + i;

        dvContainer.Controls.Add(Ddl_Item);
        dvContainer.Controls.Add(Txt_Quantity);

        dvContainer.Controls.Add(Ddl_Unit);
        dvContainer.Controls.Add(Txt_Available);

    }
    countTimes = countTimes + 1;

    ViewState.Add("countTimes", countTimes);
}

}

2 个答案:

答案 0 :(得分:0)

您正在div中添加控件,而不是在表中并且彼此相邻。要在控件周围添加一些html结构,可以在文本中使用html创建文字对象,并在控件之前/之后添加它们(例如,您可以添加<br/>

答案 1 :(得分:0)

我认为你想要的是HtmlGenericControl http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx

这将允许您添加设置布局所需的表,您当然可以将每个控件包装在div中并使用CSS控制布局。