Postback question

K

Kevin Blount

I'm trying to create a mutli-form page on my website, but having trouble
with the PostBack stuff. And yes, I'm new to C# - my background of 8yrs
is original ASP, and I'm getting easily confused now... which is most
aggravating

My sample test code is below...

The idea is that the page can have 2 or even 3 panels to show different
form elements, and then I hide all the panels and process the
information gather from the forms.

The bit that's failing right now is finding out of the form elements
shown in panel2 have been submitted, then hiding both panels to begin
processing the form fields. I did try:

if (panel2.Visible == true)...

but that failed as for each postback it was true; I also tried searching
for a way to check which button was pressed, but that failed for me too..

basically I'm stuck.. I'm trying this PostBack things as 5 days of
trying to get another method to work has finally got the better of me,
and I'm looking for a new way to have multiple forms presented and
submitted to a single processing page (kinda like a wizard.. but I' not
using the Wizard Control)


any suggestions??

Code:
<form action="" method="post" name="pageform" id="pageform" runat="server">
<%
if (IsPostBack)
{
   //what 'if' goes here to show panel2 if panel1 forms are submitted
   {
     panel2.Visible = true;
     panel1.Visible = false;
   }
   //what 'if' or 'else' goes here to hide both panels if panel2 form 
fields are submitted??
   {
     panel2.Visible = false;
     panel1.Visible = false;
     //call method(?) to process form fields
   }
}
else
{
   panel2.Visible = false;
   panel1.Visible = true;
}

%>
<asp:HiddenField ID="stepField" value=""></asp:hiddenfield>

<asp:Panel ID="panel1" runat="server">
<asp:TextBox Columns="20" ID="text1" runat="server" 
TextMode="SingleLine">1</asp:TextBox>
<asp:Button ID="panel1_btn" runat="server" Text="Continue"></asp:Button>
</asp:Panel>

<asp:Panel ID="panel2" runat="server">
<asp:TextBox Columns="20" ID="text2" runat="server" 
TextMode="SingleLine">2</asp:TextBox>
<asp:Button ID="panel2_btn" runat="server" Text="Finish"></asp:Button>
</asp:Panel>

</form>
 
1

1388-2/HB

If I had to do that I'd probably build & place all the controls myself from
code behind. Like this:

[TheWizardPage.aspx]

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="TheWizardPage.aspx.vb"
Inherits="TheWebApplication.TheWizardPage" %>

[TheWizardPage.aspx.vb]

Public Class TheWizardPage
Inherits System.Web.UI.Page

Private pnlPanel1 As New Panel
Private txtTextbox1 As New TextBox
Private WithEvents btnSubmitPanel1 As New Button

Private pnlPanel2 As New Panel
Private txtTextbox2 As New TextBox
Private WithEvents btnSubmitPanel2 As New Button

Private pnlPanel3 As New Panel
Private txtTextbox3 As New TextBox
Private WithEvents btnSubmitPanel3 As New Button

Private objForm As New HTMLForm

Private Sub Page_Init(ByVal sender as System.Object, Byval e as
System.EventArgs)

Me.btnSubmitPanel1.Text = "Button 1"
Me.btnSubmitPanel2.Text = "Button 2"
Me.btnSubmitPanel3.Text = "Button 3"

With Me.pnlPanel1

.Controls.Add(Me.txtTextBox1)
.Controls.Add(Me.btnSubmitPanel1)
.Visible = Not Page.IsPostBack

End With

With Me.pnlPanel2

.Controls.Add(Me.txtTextBox2)
.Controls.Add(Me.btnSubmitPanel2)
.Visible = False

End With

With Me.pnlPanel3

.Controls.Add(Me.txtTextBox3)
.Controls.Add(Me.btnSubmitPanel3)
.Visible = False

End With

'We'll either leave Panel1 visible, or change which one is visible later
on in the page cycle.

With Me.objForm

.Controls.Add(Me.pnlPanel1)
.Controls.Add(Me.pnlPanel2)
.Controls.Add(Me.pnlPanel3)

End With

Me.Controls.Add(New LiteralControl("Here comes the form..."))
Me.Controls.Add(Me.objForm)
Me.Controls.Add(New LiteralControl("...there went the form"))

End Sub

Private Sub btnSubmitPanel1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmitPanel1.Click

'I know Button1 was clicked because this event specifically handles it
being clicked on postback

MyClass.MyMethod(Me.txtTextbox1.Text)

'Change which panels are visible before the page gets rendered.
Me.pnlPanel1.Visible=False
Me.pnlPanel2.Visible=True
Me.pnlPanel3.Visible=False

End Sub

Private Sub btnSubmitPanel2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmitPanel2.Click

'I know Button2 was clicked because this event specifically handles it
being clicked on postback

MyClass.MyMethod(Me.txtTextbox2.Text)

'Change which panels are visible before the page gets rendered.
Me.pnlPanel1.Visible=False
Me.pnlPanel2.Visible=False
Me.pnlPanel3.Visible=True

End Sub

Private Sub btnSubmitPanel3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmitPanel3.Click

'I know Button3 was clicked because this event specifically handles it
being clicked on postback

MyClass.MyMethod(Me.txtTextbox3.Text)

Response.Redirect("WizardConfirmation.aspx")

End Sub
 
K

Kevin Blount

Thanks for the response, er... 1388-2/HB (name mean something?)

I ended up finding a pretty much perfect example used by someone else in
this newsgroup, when asking for help with Validation, and I was able to
convert their VB.NET to C# and I now have a working test script (below)

cheers

Kevin (name means Warrior.. apparently o_O)


<%@ Page Debug="true" Language="C#" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script runat="server">
private void Page_Load(object sender, EventArgs e)
{
hideAllPanels();
if (!IsPostBack)
{
panel1.Visible = true;
}
}

private void button_click(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
if (e.CommandName == "show_panel2")
{
panel2.Visible = true;
}
else if(e.CommandName == "show_panel3")
{
panel3.Visible = true;
}
}

private void hideAllPanels() {
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = false;
}
</script>
<form action="" method="post" name="pageform" id="pageform" runat="server">
<asp:panel ID="panel1" runat="server" Wrap="true">
<h2>Panel 1 - username</h2>
<asp:TextBox ID="username" runat="server" />
<asp:Button ID="show_panel2" CommandName="show_panel2"
OnCommand="button_click" runat="server" Text="Next >" />
</asp:panel>


<asp:panel ID="panel2" runat="server" Wrap="true">
<h2>Panel 2 - password</h2>
<asp:TextBox ID="password" runat="server" />
<asp:Button ID="show_panel3" CommandName="show_panel3"
OnCommand="button_click" runat="server" Text="Next >" />
</asp:panel>

<asp:panel ID="panel3" runat="server" Wrap="true">
<h2>Panel 3 - something</h2>
<asp:TextBox ID="other" runat="server" />
<asp:Button ID="processForm" CommandName="processForm"
OnCommand="button_click" runat="server" Text="Finish" />
</asp:panel>
</form>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top