Introductory tutorial on using objects to pass information from page to page

Passing asp.net information between pages can get quite messy, unless you clean it up using objects

 Recently on a forum I was asked to help out a user on passing information between pages using asp.net.  After some contemplation, I figured the cleanest way to do this would be through an object passed as a variable.  Basically, take in some information on page one, manipulate it, and regurgitate it on page two.

 

 For this tutorial, I will be referencing the demo that I have uploaded: passing_with_objects.zip (4.06 kb) Also, this tutorial assumes you are using Visual Studio or the Free version: Visual Web Developer Express

 

First we need to think of how we are going to pass the information. Since we are going to another page, we have to store it somewhere as html is stateless, and you are using .net controls and not a regular POST. So, we have a few options:


1. Store variables for each value
2. Construct a single object and store that
3. Use a PreviousPage declaration
4. Use a querystring

 

I vote for 2, as it is cleaner, and more scalable. Next you need to make an object by right clicking in your solution explorer, clicking 'add new item' and choose 'class' - name it what you will, I chose 'employee'. (its going to ask you to put it in the app_code folder, you want to do this).  Now you need to define what the object has for properties, and how you are going to set and access them.  This is what I came up with:

//here you declare all of the properties that each 'employee' object will have
string name;
string id;
//constructor
public employee() { }
// this one actually builds an employee object - requires 2 strings as of now, but you can add more
public employee(string nameInput, string idInput)
{
  name = nameInput;
  id = idInput;
}
// methods - returns what will be pulled from the employee obect here
public string showName() { return name; }
public string showId() { return id; }

With that you can see that an employee has both an ID and a Name property, they are both strings and are set by calling employee(name, id).  So now we have our object class... let's use it.


We have 2 textboxes, txtName and txtId, that will hold, you guessed it, name and id. I also added RequiredFieldValidators to try and make sure we get no errors, requiring the user to enter values before they submit.

 

Add a submit button and double click on it in design mode, that will auto-populate a Click event in your code behind to tell your page what to do when it is clicked. here you will make an empoyee object, and then set it to a session variable - then kick it to the display page:

protected void btnSubmit_Click(object sender, EventArgs e)
{//builds new employee object
 
employee newEmployee = newemployee(txtName.Text, txtId.Text);  Session["employee"] = newEmployee; 

  //put it in session state memory
  Response.Redirect("DisplayData_Class.aspx");//sends you to the next page
}

And then we make the showEmployee() method by calling the methods we made in teh employee class we made earlier:
protected void showEmployeeInfo()
{
  employee passedEmployee = new employee();//makes the new object
  //copies the one you just made
  passedEmployee = (
employee)Session["employee"];//display info
  lblName.Text = passedEmployee.showName();
  lblId.Text = passedEmployee.showId();
}

And boom, you are all done. Now just go through and add the properties to the object you need and ways to access and set them and you will be done in no time.

 


Comments (2) -

  • Passing objects between pages by abusing the session scope is no good idea in my opinion.
    Research on the following: Requestscope, Pagescrope, Sessionscope and Applicationscope.

Add comment

Loading