tags:

views:

283

answers:

9

is it possible to display, with a click of a button an entirely different form on the same aspx page? please take note that although i have experience with vb.net, i have almost none with asp.net. thank you very much for your responses

+1  A: 

If I understand, what you need is, on the click event:

response.redirect "newpage.aspx"

tekBlues
yes perhaps thats it, but is there a way to do this without redirecting to a different page?
I__
Server.Transfer? That technically doesn't do a redirect.
RichardOD
+1  A: 

Create each of the forms on the same page, one with visible=true and the other visible=false, and when the user clicks on the appropriate button, switch the visibilities.

<form id="Form1" runat="server" visible="true">
<div>
    <asp:Button ID="Button1" runat="server" Text="Show Form 2" onclick="Button1_Click" />
</div>
</form>

<form id="Form2" runat="server" visible="false">
<div>
    <asp:Button ID="Button2" runat="server" Text="Show Form 1" onclick="Button2_Click" />
</div>
</form>

And in the code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    this.form2.Visible = true;
    this.form1.Visible = false;
}

protected void Button2_Click(object sender, EventArgs e)
{
    this.form2.Visible = false;
    this.form1.Visible = true;
}
Matthew Jones
it says Error 1 'Button2_Click' is not a member of 'ASP.default_aspx'.
I__
are you getting the same error?
I__
Make absolutely sure you have the onclick attribute set correctly and that the Button2_Click exists in the code behind. These are the most obvious causes of this problem.
Matthew Jones
+1  A: 

You could do it with CSS/Javascript, here is what I would do first:

1) code up two forms, place them in a separate divs 2) using CSS hide one div on page load 3) then place a button on the page, in the onlick button event unhide the second form and hide the first one.

Make sure that you only have ONE form tag, but 2 divs inside it which you will hide/unhide. Keep in Mind that that the form can only be submitted to its own page, that's asp.net.

in your HTML:

    <form runat="server" id="myForm">

<div id="myForm1">
<! -- form 1 code goes here -- !>
</div>

<div id="myForm2">
<! -- form 2 code goes here -- !>
</div>

<input type="button" onclick="toggleVisibility();" />

</form>

Then in your CSS

#myForm1 {
display: none;
}

Then ToggleVisibility() will change the display attribute of divs.

gnomixa
+1  A: 

Probably not the most "Ajaxy" solution, but you could use an iframe, with the src set to the forms location.

Jason Watts
+1  A: 

It is NOT allowed to have more then 1 form runat="server" on an asp.net page. What you could do, is create 2 panels on your page, 1 with the Visible property set to false. Then when a button is clicked in the event handler you set the Visisble property to true, while setting the other 1 to false. Wrap the Panel in an UpdatePanel to get rid of the postback.

<asp:UpdatePanel><ContentTemplate>
<asp:Panel ID="pnl1" runat="server">
  <asp:Button OnClick="Button_CLick" />
</asp:Panel>
<asp:Panel ID="pnl2" runat="server" Visible="false">
</asp:Panel></ContentTemplate></asp:UpdatePanel>

the code in the Button_CLick handler would then be pnl1.Visible = false; pnl2.Visible = true;

Colin
Really? What does not allow it? Cause I just tried that on my local machine and it worked fine.
Matthew Jones
What, thats weird, it used to never work (so thats why I never tried it anymore). ASP.NET would not be able to distinguising which form it is in. Page.Form would not work anymore for instance.
Colin
You can only have ONE form with runat attribute VISIBLE at a time. See http://msdn.microsoft.com/en-ca/magazine/cc163736.aspx
gnomixa
see this link http://msdn.microsoft.com/en-us/magazine/cc164151.aspx
Colin
In other words you may have several coded up with visibility=false, but only 1 can be visible at any given time
gnomixa
Cool, didn;t know that!
Colin
+1  A: 

You should be aware of ASP.NET's MultiView control. It does require a postback to change views, and it is kinda heavy on the ViewState, but its an option to consider.

Frank Schwieterman
+1  A: 

Well, there's several ways to go about that I suppose. Riffing off tekBlues, you can do a Server.Transfer "yourpage.aspx". You can then use the PreviousPage property to get to data from the old page.

You can use user controls and a placeholder on the main page. Of course dynamically loaded controls holds extra complexity.

You could use a MultiView control. Asp.Net will maintain all vars for you. Useful for the quick and dirty.

These are all webform solutions though. If you're looking for an AJAX solution, might need to keep on looking for answers.

KeithA
+1  A: 

Use AJAX to load the content of another page into the same page.

Use Response.Redirect or Server.Transfer to move to the next page.

Bhaskar
+1  A: 

I would use and in your code behind, load up the page and then place it in the placeHolder. And then hide the old form using javascript. The idea the other person said would also work, but I like using the placeholder, myself.

I think it's all really determinate on what you want to do with the forms and how badly you would want the code for the other form laying on the page, or not.

MattK311