如何根据列表中的no.of项在div标签内添加新的html元素?

时间:2016-11-20 04:14:28

标签: html asp.net

issue.aspx

<div id="model_input_stock" runat="server" class="form-group well">Dynamically need to insert new textbox and lable with in new div</div>

Issue.aspx.cs

protected void Button_Grid_DT_Assign_Click(object sender, EventArgs e)
{
    //use assigned in selecting issued quantity view
    foreach(var item in StockCode_List)
    {
        model_input_stock.InnerHtml.???
    }  
}

他们是最好的替代方式,我是ASP.NET的新手吗?

2 个答案:

答案 0 :(得分:1)

您可以先在div中添加一个ASP.net面板,然后在foreach循环内部初始化一个新文本框并将其添加到面板中:

Textbox txt = new Textbox();
txt.id = "txt1";
Txt.Text = "My Text";
Panel.Controls.Add(txt);

答案 1 :(得分:0)

使用以下代码执行此操作:

Default.aspx的:

<div>
   <asp:TextBox ID="txtRows" runat="server"></asp:TextBox>
   <asp:Button SkinID="b" ID="btnAddRow" runat="server" Text="Click To Proceed" OnClick="btnAddRow_Click" Width="220px" />
   <asp:GridView SkinID="grv" ID="GridViewProducts" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
      <AlternatingRowStyle BackColor="White" />
        <Columns>
          <asp:TemplateField HeaderText="SL No.">
            <ItemTemplate>
               <%#Container.DataItemIndex + 1%>
            </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Category">
            <ItemTemplate>
              <asp:DropDownList ID="ddlCategoryID" runat="server" AutoPostBack="True">
              </asp:DropDownList>
            </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Product Name">
            <ItemTemplate>
              <asp:DropDownList ID="ddlItemID" runat="server" AutoPostBack="True">
              </asp:DropDownList>
            </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
              <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Unit Type">
            <ItemTemplate>
               <asp:DropDownList ID="ddlUnitTypeID" runat="server" AutoPostBack="True">
               </asp:DropDownList>
            </ItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:TextBox ID="txtDes" runat="server"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
                </Columns>
     </asp:GridView>
</div>

<div style="padding: 10px 0px;">
   <asp:Panel ID="Panel1" runat="server" Visible="false">
       <asp:Button ID="btnSave" runat="server" Text="Submit" OnClick="btnSave_Click" />
       <asp:Label ID="lblMsg" runat="server"></asp:Label>
  </asp:Panel>
</div>

在代码隐藏中,Default.aspx.cs:

private void AddRowsToGrid()
{
    List<int> noofRows = new List<int>();

    int noOfInputs = Convert.ToInt32(txtRows.Text);

    for (int i = 0; i < noOfInputs; i++)
    {
       noofRows.Add(i);
    }

    GridViewProducts.DataSource = noofRows;
    GridViewProducts.DataBind();

    if (GridViewProducts.Rows.Count > 0)
    {
       Panel1.Visible = true;
    }
    else
    {
       Panel1.Visible = false;
    }
}

protected void btnAddRow_Click(object sender, EventArgs e)
{
   AddRowsToGrid();
}

最后,您将输入以下输出10:

{{3}}