Sagar Sagar - 14 days ago 5
HTML Question

How to add new html elements inside div tag, on basis of no.of items in list?

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.???
}
}


Is their any best alternative way, i'm new in ASP.NET??

Answer

Use the following code to do so:

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>

In the code-behind, 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();
}

Finally you'll have the following output giving input 10:

Sample_AddRow