naspinski.net

software development

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:

 


css Tooltips :: tutorial

A slick way to make rollover tooltips with nothing other than a little css and some html - also works well for image thumbnail rollovers as well

In the past, I have made tooltips with JavaScript, and that can get lengthy and messy - I prefer to use JavaScript as little as possible. So I recently figured out a way to make tooltips and picture enlarging rollovers that is very simple and uses nothing but css and html.

Ok, first thing we need to do is to understand how this is going to work: basically, you are going to embed your tooltip with an element inside your link; for this example, I chose span as it is already there and easy to work with. Then we must hide that text with display:none; in css.

Now I want the tooltip to stay close to the link that you hover over to get it (you could use this to put the link anywhere on the page as well) so I have to make the link relative, and the embedded span will be positioned absolutely, and sice it is positioned absolutely to the relatively positioned link, it will appear relative to the link (wow... that looks confusing when I put it down in text).

Now with that out of the way, we just have to add a :hover state to the class and change it's display to display:block; to make it visible. Add a little formatting and you get the following css:

a.tooltip{position:relative;}
   a.tooltip:hover{z-index:25;}
   a.tooltip span{
     display: none;position:absolute;top:1em; left:1em;

     /* the line above is all that is necessary in a.tooltip span the rest is formatting - you can alter top and left */
     padding:2px; border:1px solid Black; width:100px; background-color:Gray;}

   a.tooltip:hover span{display:block;}
You will notice that there is a z-index value in there. I picked a rather large number, but that is so the tooltip will always be visible over whatever you have on the page.

The css is now out of the way, time to move on to the html. All of the hard work is done, now we simply need to make a link with an embedded span which to contain our tooltip:
<a class="tooltip" href="" >
   Roll over this
   <span>For this kickin' tooltip!</span>
</a>
And there you have it, a simple css rollover tooltip:
Roll over this For this kickin' tooltip!

*note: If you dont want the rollovers to appear clickable (usually the finger cursor) you can add cursor:default; to the a.tooltip:hover class.

Similiarly, you can do the same with images. For this example, I am using a full size imaged resized for the thumbnail, but you may want to use a separate smaller image so the preview loads faster for your user; its up to you: I changed the css class, removed the formatting and lsightly edited the positioning to get this for the css:
a.img_tooltip{position:relative;}
   a.img_tooltip:hover{z-index:25;}
   a.img_tooltip span{display: none;position:absolute;top:-1em; left:5em; }

   a.img_tooltip:hover span{display:block;}
And here is the html for an image rollover:
<a class="img_tooltip" href="" >
   <img src="images/image.jpg" alt="thumbnail" style="width:100px;" />
   <span><img src="images/image.jpg" alt="full image"  /></span>
</a>
As you can see, I set width:100px; as I am using a full sized image and I wanted to shrink it. Here is your image rollover
small image full image

And there you have it, a simple way to get css rollover tooltips. They can be pretty useful when you want to stick something in your page but it may be intrusive or ugly to be there all the time.

A very simple example can be downloaded from the steal some code repository or just pick it up here: css_tooltips.zip (67.91 kb)