Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Forms, Form.action and Form data 1

Status
Not open for further replies.

RedLion

Programmer
Sep 13, 2000
342
NL
Hello,

I'm used to work with ColdFusion or PHP. Now I am trying to do the same things with ASP.NET, but I can't figure out how it can / should be done with ASP.NET. I appreciate any help!

I've got form 1: index.aspx

<%@ Page language=&quot;c#&quot; Codebehind=&quot;index.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;AdresApp.index&quot; %>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; >
<HTML>
<HEAD>
<title>Addres applicatie</title>
<meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 7.0&quot;>
<meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; </HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;index&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:Button id=&quot;cmdSubmit&quot; style=&quot;Z-INDEX: 101; LEFT: 279px; POSITION: absolute; TOP: 96px&quot; runat=&quot;server&quot; Text=&quot;Button&quot;></asp:Button>
<asp:Label id=&quot;lblName&quot; style=&quot;Z-INDEX: 102; LEFT: 139px; POSITION: absolute; TOP: 52px&quot; runat=&quot;server&quot;>Name:</asp:Label>
<asp:TextBox id=&quot;txtName&quot; style=&quot;Z-INDEX: 103; LEFT: 183px; POSITION: absolute; TOP: 50px&quot; runat=&quot;server&quot;></asp:TextBox>
</form>
</body>
</HTML>


index.aspx.cs

public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button cmdSubmit;
protected System.Web.UI.WebControls.Label lblName;
protected System.Web.UI.WebControls.TextBox txtName;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void cmdSubmit_Click(object sender, System.EventArgs e)
{
ViewState[&quot;cName&quot;] = txtName.Text;
Server.Transfer(&quot;WebForm1.aspx&quot;);
}
}


WebForm1.aspx

<%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;AdresApp.WebForm1&quot; %>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 7.0&quot;>
<meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; </HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:Label id=&quot;lblPostedName&quot; style=&quot;Z-INDEX: 104; LEFT: 174px; POSITION: absolute; TOP: 94px&quot; runat=&quot;server&quot;></asp:Label>
</form>
</body>
</HTML>


WebForm1.aspx.cs

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblPostedName;

private void Page_Load(object sender, System.EventArgs e)
{
lblPostedName.Text = (String) ViewState[&quot;cName&quot;];
}

}



I start index.aspx I type in my name Charl click the button cmdSubmit. Then the page WebForm1.aspx should be loaded with my name putted into the label lblPostedName.Text.

There are two things I don't know how to realize it:
1. How to change the action property from the html form to another page?
2. I've seen in another post from someone else that I should use Server.Transfer(&quot;WebForm1.aspx&quot;);, but my form variable isn't posted to the next page? How to do this?


Thanks,

Charl
 
You can't change the action of a form with the attribute:
runat=server

And so, just abandon the idea. There are work-arounds where you can make another form on the page, and point that to another page (like classic ASP), but then you have to get your server values into that form, etc... etc... and it simply isn't worth the headache.

Now, as for server.transfer, I'd recommend a response.redirect, since server.transfer has limitations on what pages it can transfer to, and the client is completely unaware of the transfer if you use server.transfer, and therefore, if they hit &quot;refresh&quot;, then it loads the wrong page.

Response.Redirect(url) is not as efficient, but it works well under any circumstance, and you really don't take much more of a hit on performance by using it.

That being said, in order to transfer your variables to another page, there are three main options (In my opinion, the best ones, and I'm sure others will differ with me)

(1)tack them onto querystring
--lowest security; good method when info is not sensitive

(2)put them into session state
--higher security; hard on server resources if lots and lots of them are used. Just use them sparingly. Additionally, cookieless session state can be opted for, as well as sql session state. Check the docs for details, and pick your poison.

(3)use asp:panel to make the page behave as though the page has changed, but it actually has not, but simply shows/hides different portions of the paged based on your wishes. I like this design pattern, as long as the page doesn't get too big an un-manageable. You can also check out the docs or google for more information on this method.

:)
paul
penny1.gif
penny1.gif
 
Hi Paul,

Thanks for your response and fresh look on the different approaches!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top