News of the world

This blog is in the first case meant for myself so I don't forget interesting stuff. So you will find lots of useless topics, but there will certainly be also readings that might interest you. Lots of the items will go about my family, things we do or don't do but also about software, cars, gadgets, music, etc ... .

21 nov 2006

Newbie: Posting parameters between ASP.NET 2.0 forms

For a project at work I started of experimenting with ASP.NET 2.0. I decided to use ASP.NET because I needed XHTML compliant forms and I thought ASP.NET was just plain easy dragging and dropping of components onto a form and my application is finished. Well damn that's not the case, although I can call myself a seasoned Java/JSP/Servlet developer I was not able to create an app in five minutes with drag and drop. ASP.NET doesn't give what you expect when you know how to write web applications in some other language / framework. To start with I wasn't able to post parameters from one page to another, it isn't just setting the action parameter on the form tag and creating a submit button ... . It is a bit less straight forward and this is what I did to post my parameters to another form.

I created two ASP.NET pages: page1.aspx and page2.aspx. The first page has two textboxes firstnameTextBox and lastnameTextBox and a button two submit the form. The second page has just a label nameLabel that will display the values of the two textboxes of the first page.

Now the first thing I want to do, is post my parameters on the first page to the second one. Apparently there are two ways for doing this:

  1. Select the submit button, look in the properties for PostBackUrl and select the page you want to submit to.
  2. The other solution is double clicking the button and then writing the following code in the event:

    protected void Submit_Click(object sender, EventArgs e)
    {
        Server.Transfer("Page2.aspx");
    }

Don't knwo yet what the difference between the two is but maybe I will come back to this later or if some of you have suggestions feel free to write me.

Ok, now I am able to navigating from my first page to the second one. Now I want to pass the values of my two textboxes to the second page. The thing is ASP.NET as a close focus on OO-programming so why not try to access the textboxes from my first page when I am on the second page. For this I created two properties in the code-behind file of my firstpage and they look like this:

public string Firstname
{
get
{
return firstnameTextBox.Text;
}
}

public string LastName
{
get
{
return lastnameTextBox.Text;
}
}

Now on my second page I need to be able to access my first page for this I need to add the following directive to the aspx file:

<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>

This directive will give us a strongly typed PreviousPage. And in the Page_Load event of the second page I wrote the following code to access the properties on the first page and assemble them together in the label on the second page:

if(this.PreviousPage != null)
{
nameLabel.Text = this.PreviousPage.Firstname + " " + this.PreviousPage.Lastname;
}

Phew that was hard figuring out how to do parameter passing in ASP.NET but it's a nice programming model once you get the hang of it.

Tags:

2 Comments:

At 1:17 p.m., Blogger Unknown said...

Jan,


Its nice.Now i knew that we could expose controls value from one page to another and the page directves.

Pls keep on posting .....


Thanks

Badru

 
At 3:15 a.m., Anonymous Anoniem said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

 

Een reactie posten

<< Home