Duplicate web form on button click - c#

I have an ASP web form that includes divs, checkboxes, textboxes, buttons and dropdowns. Backend is c#.
I have an "Add" button at the bottom that when clicked, I want it to duplicate my form below the existing one.
Other than copying my form multiple times and toggling visibility based on a button click, is there some way I can put my form in a class and call on it based on the button click? Or any other way? Would think gridview is an option but I use divs within the form which is not allowed in gridview.
For Example:
<form id="form" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="add" runat="server" Text="Add" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
The Add button can be moved outside of the update panel if necessary. But I would like to duplicate the form below the original via clicking the Add button. Would also be handy if I could have a remove option as well.

Related

Radio button not visible in ASP.NET page

ASP.NET radio button is not visible in my page. I am using it inside ASP.NET ListView. I tried putting the radio button inside ASP.NET update panel content template as well. Please tell me what has gone wrong?
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:ListView ID="FraudCheckList_listView" runat="server">
<ItemTemplate>
<p class="fraud-step-title"><%#Eval("question")%></p>
<div class="row">
<asp:RadioButton
runat="server"
class="radio-inline col"
GroupName="govt"
Text="Low Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="Medium Risk"
></asp:RadioButton>
<asp:RadioButton
runat="server"
GroupName='govt'
Text="High Risk"
></asp:RadioButton>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
Since you haven't shared how your code looks like, i am just assuming certain things. Just see if you have done this or not.
void Button_Click(object sender, System.EventArgs e){RadioButton.Visible=false;}
If incase its true or something your radiobutton will not be visible. In your case it must be hidden. So, make the variable of visible as **false ** So you can just change your code to this and try it out.
This you have to do it in your main code. not in your html code.
Thank You!

Default button for 'Enter' key with a form which does not 'runat="server"'

I just created an ASP.NET, C# web application and for the first time i used a master page.
I learnt that you can't have two forms that runat "server" because it causes an error, so for the form (which uses for login) at the top of the master page i used a runat="server" and for a form in the content page i removed the runat attribute.
Now i cannot use the 'defaultbutton' attribute and the "Enter" key will go to the login button at the master page even if a text field in the content page is now active.
What is the solution to this problem?
Thanks!
You can use multiple Panel controls for this.
<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
...
<asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
...
<asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>
how to set a default 'enter' on a certain button

Fail to continously add new control into Panel/Placeholder during partial postback

I want to create a dynamic form where user has a button to click on it.
When the button clicked, it will trigger a partial postback and add a new control into a placeholder.
My problem is, when the button is clicked for the first time, it able to create a new control in the placeholder. But it will not create more than that even though i have clicked the button for few times.
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Add images" OnClick="Button1_Click" />
<asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
<br />
<asp:Panel ID="PlaceHolder1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(new FileUpload());
}
Unless a dynamically added control is added at the preinit or init stage, it does not persist beyond a post back. So when the button is clicked for the second time, the first added control is lost and then the button logic adds a control again, leaving you with one control every time. For a control added after the init stage, you would need to store its state and recreate it on each postback. This is described int this article:
http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
You may also be able to achieve your goals without using dynamically added controls. One possible approach is to use a ListView control, add a FileUpload control to its ItemTemplate and add a new record to the list view data source every time the button is clicked.

Button inside asp:panel is not working properly

I have an ajax extender in my aspx page.
I want to make the button do something in code behind when is pushed but my button inside the panel doesn't work.I created another button outside the panel and wrote the same thing to do and is working.
This is my button inside the panel:
<ajax:AccordionPane ID="AccordionPane1" runat="server">
<Header>Adauga titlu</Header>
<Content>
<asp:Panel ID="UserReg" runat="server" DefaultButton="but">
<cc1:Editor ID="Editor1" runat="server" width="500px"/>
<br /><br />
<asp:Button runat="server" ID="but" Text="Adauga Titlu" />
</asp:Panel>
</Content>
</ajax:AccordionPane>
This button is not working but the other button outside is working and they both do the same thing.Can you provide a solution?
Perhaps you are simply missing the OnClick attribute, like this:
<asp:Button runat="server" ID="but" Text="Adauga Titlu" OnClick="but_Click" />
Have you included the Asp ScriptManager?
http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx

ASP.NET AJAX Control Toolkit: Show a ModalPopup and then do PostBack

I want to show a modal popup when a user click on an asp button. The user must select an option of a panel. The value of the option selected must be saved to an input hidden and then the asp.net button must do a PostBack.
Can I do that?
Thank you!
It is possible for a ModalPopupExtender to be displayed using a postback. You'll need an invisible target control. The extender is attached to this hidden control.
<asp:Button runat="server" ID="btnShowModal" Text="Show"
OnClick="btnShowModal_Click" />
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolKit:ModalPopupExtender ID="Modal1" runat="server"
TargetControlID="HiddenForModal" PopupControlID="PopupPanel" />
In your message handler in code-behind, you'll show the ModalPopupExtender:
Modal1.Show();
And in the code you're using to dismiss the Modal, call the ModalPopupExtender's Hide function:
Modal1.Hide();
I use this method for showing a modal that displays detailed data that I retrieve from a database based on what's selected in a GridView.
Add your Button or LinkButton
<asp:Button ID="MyButton" Text="Click Here" runat="server" />
Add a Panel to hold your options with a DropDownList
<asp:Panel ID="MyPanel" runat="server">
<asp:DropDownList ID="MyDropDown" runat="server">
<asp:ListItem Value="1" Text="Option 1" />
</asp:DropDownList>
<asp:Button ID="SaveBtn" Text="Save" OnClick="Save_Click" runat="server" />
<asp:Button ID="CancelBtn" Text="Cancel" runat="server" />
</asp:Panel>
Add your ModelPopupExtender
<act:ModalPopupExtender ID="Mpe1" TargetControlID="MyButton"
CancelControlID="CancelBtn" PopupControlID="MyPanel" runat="server" />
Then add your code behind to the SaveBtn Button
public void SaveBtn_Click(object sender, EventArgs e) {
string selectedOption = MyDropDown.SelectedValue;
}
Finally, I've decided to use jQuery to show a ModalPopUp. The following question has the answer to this question:
jQuery UI's Dialog doesn't work on ASP.NET
If you are not agree, tell me.

Categories

Resources