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.

 


Sending email programmatically with gmail :: tutorial

worry about smtp servers/settings no longer; completely portable code for sending email with gmail and asp.net

I have used a ton of different hosts, and it always seems to be a hassle to find out their information on their smtp servers... not to mention all of your programs are broken if you move them.  With .net, you don't need to use anything but gmail's own protocols in order to make fully portable code that sends emails, and it is oh-so-simple!  I use this from everything from contact pages to email notifications to myself on errors and such - hope it helps.

 

First thing you need to do is declare you will be using the System.Net.Mail namespace as the System.Web.Mail is deprecated as of the past year or so.

 

Next, you simply have to make a new MailMessage and set its properties like To, From, Body, Subject, etc.  here is an example with all static input:

 

MailMessage email = new MailMessage();
email.To.Add("to@address.com");
MailAddress maFrom = new MailAddress("from@address.com");
email.From = maFrom;
email.Body = txtMessage.Text;
email.IsBodyHtml = true;//this is only if you are sending an html message
email.Subject = txtSubject.Text;

 

Now that the message is made, all you have to do is set your smtp credentials, and push the message out:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 25);
smtp.EnableSsl = true;

smtp.Credentials = new System.Net.NetworkCredential("address@gmail.com", "password");

smtp.Send(email);

 

That's it!  It is just that easy... 

 

You can get a fully working example here: