[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I improve my session variable code?

Posted on 2009-04-23
9
Medium Priority
?
305 Views
Last Modified: 2012-05-06
I am using a session variable to transfer an email user name from one page to another where it is concatenated with the domain part of the address to form a whole email address.  Eventually, this will used to send an email from a form.

What I have now is attached in code.  I feel like it's messy or inefficient.  A linkbutton is used for the email link, but an invisible label is used to hold the value of the email username.  Is there a cleaner way to do this?
<!-- The page with the link: -->
 
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
 
<script runat="server" language="c#">
 
protected void lnkEmailLink_Click(object sender, EventArgs e)
{
	Session["KeyName"] = lblEmailUser.Text;
	Response.Redirect("emailForm.aspx");
}
 
</script>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" runat="server">
 
<asp:LinkButton ID="lnkEmailLink" runat="server" OnClick="lnkEmailLink_Click" Text="Email"></asp:LinkButton><asp:Label ID="lblEmailUser" runat="server" Text="jcoulson" Visible="false"></asp:Label>
 
</form>
</body>
</html>
 
<!-- The page that receives the data: -->
 
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
 
<script runat="server" language="c#">
 
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["KeyName"] != null)
        {
            //get the Session value
            string sValue = Session["KeyName"].ToString();
			lblEmailUser2.Text = sValue;
			string strDomain = "@co.frederick.va.us";
			string strResult = sValue + strDomain;
			lblResult.Text = strResult;
        }
		else
		{
			lblEmailUser2.Text = "Nothing!";
		}			
		
    }
	
</script>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" runat="server">
 
<p>TEST PAGE.</p>
 
<asp:Label ID="lblEmailUser2" runat="server"></asp:Label>
<br />
<asp:Label ID="lblResult" runat="server"></asp:Label>
 
</form>
</body>
</html>

Open in new window

0
Comment
Question by:mrcoulson
  • 5
  • 4
9 Comments
 
LVL 9

Expert Comment

by:kprestage
ID: 24214829

Response.Redirect("emailForm.aspx?user=" + lblEmailUser.Text);
 
 
string sValue = Request["user"].ToString();

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 24215403
Maybe I wasn't clear enough.  Is there a way to do this WITHOUT using lblEmailUser?  It seems like poor programming to stick that invisible label in there.  Is there some other place the username could go besides that label?
0
 
LVL 9

Expert Comment

by:kprestage
ID: 24215613
You can use a hidden field, which is essentially the same as a hidden label, but is the more "standard" way used.  You can also use session, but in my opinion the hidden field is better than using session.  Is there a reason you are worried about the hidden label?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:mrcoulson
ID: 24215692
The reason: I'm still a novice programmer in many ways and I have no idea how it will affect my pages down the road.  For example, what does visible="false" really mean?  Will it be as if nothing is there or will there be a blank area as if the space for the label is reserved?  
0
 

Author Comment

by:mrcoulson
ID: 24215788
How about this?  Is this okay?  It seems to work.
protected void lnkEmailLink_Click(object sender, EventArgs e)
{
     // Session["KeyName"] = lblEmailUser.Text;
     Server.Transfer("emailForm.aspx?user=" + lblEmailUser.Text);
}
 
 
protected void Page_Load(object sender, EventArgs e)
{
     string sValue = Request["user"].ToString();
     lblEmailUser2.Text = sValue;
     string strDomain = "@co.frederick.va.us";
     string strResult = sValue + strDomain;
     lblResult.Text = strResult;			
}

Open in new window

0
 
LVL 9

Accepted Solution

by:
kprestage earned 2000 total points
ID: 24215791
This is why you would want to use a hidden field...they are made specifically for holding data that should not be shown to the user (unless they view source in their browser).  They do not take up space on the page either.  
0
 

Author Comment

by:mrcoulson
ID: 24215801
P.S.  I chose server.transfer because it doesn't display the username in the URL.  Security isn't that important, but if I can hide it, I'd like to.

Jeremy
0
 

Author Closing Comment

by:mrcoulson
ID: 31573746
Hey!  That's exactly what I wanted to hear!
0
 
LVL 9

Expert Comment

by:kprestage
ID: 24215848
Yep...that should work, but I would still move to the hidden field just so you are in the habit of doing it that way.  Also, anticipate people trying to get around your query strings.  So you might want to check the there is a Request["user"] variable.

if (Request["user"] != null)
{
     string sValue = Request["user"].ToString();
     lblEmailUser2.Text = sValue;
     string strDomain = "@co.frederick.va.us";
     string strResult = sValue + strDomain;
     lblResult.Text = strResult; 
}
else
{
     lblResult.Text = "No email was provided, please <a href=[url to previous page]>Click Here</a>.";
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question