fokz8 fokz8 - 3 days ago 6
jQuery Question

How to load/show asp.net form(panel) on click

I have a form with some data inside my .aspx webform

I also have a button. When i click that button, i need to load another, exactly the same form, but without refreshing the site, so i wouldn't loose any data in the previous form.

How can i do this?

<form id="form1" runat="server">
<asp:Label ID="labelOpis" runat="server" Text="Opis izdelka"></asp:Label><br />
<asp:TextBox ID="inputTextArea" class="form-control" runat="server"></asp:TextBox><br />
<span style="color:red"><asp:Label ID="errorOpis" runat="server"></asp:Label></span><br />
<asp:Label ID="labelKolicina" runat="server" Text="Količina"></asp:Label><br />
<asp:TextBox ID="inputKolicina" type="number" class="form-control" runat="server"></asp:TextBox><br />




<button id="btnAddNewProduct" class="form-control">Add new product</button><br />

Answer

I think you are looking for dynamically added User Controls.

Add a new User Control to your project and put your form in it.

<asp:Label ID="labelOpis" runat="server" Text="Opis izdelka"></asp:Label>
<br />
<asp:TextBox ID="inputTextArea" class="form-control" runat="server"></asp:TextBox>
<br />
<span style="color: red"><asp:Label ID="errorOpis" runat="server"></asp:Label></span>
<br />
<asp:Label ID="labelKolicina" runat="server" Text="Količina"></asp:Label>
<br />
<asp:TextBox ID="inputKolicina" type="number" class="form-control" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

On the page that will contain the Controls you need to add a PlaceHolder

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<br />
<asp:Button ID="AddControl" runat="server" Text="Button" OnClick="AddControl_Click" />

Then you need to (re)create the User Controls on every Page Load and the Add Control button click.

protected void Page_Load(object sender, EventArgs e)
{
    //check if the viewstate exists and if so, build the controls
    if (ViewState["controlCount"] != null)
    {
        addControls(Convert.ToInt32(ViewState["controlCount"]));
    }
    else
    {
        //if not add just 1 control
        addControls(1);
    }
}

private void addControls(int n)
{
    //loop the amount of controls and add them to the placeholder
    for (int i = 0; i < n; i++)
    {
        UserControl1 control = (UserControl1)LoadControl("~/UserControl1.ascx");
        PlaceHolder1.Controls.Add(control);
    }

    //save the control count into a viewstate
    ViewState["controlCount"] = PlaceHolder1.Controls.Count;
}

protected void AddControl_Click(object sender, EventArgs e)
{
    //add an extra control
    addControls(1);
}
Comments