Link to home
Start Free TrialLog in
Avatar of mrcoulson
mrcoulsonFlag for United States of America

asked on

How can I improve my session variable code?

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

Avatar of kprestage
kprestage


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

Open in new window

Avatar of mrcoulson

ASKER

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?
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?
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?  
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

ASKER CERTIFIED SOLUTION
Avatar of kprestage
kprestage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Hey!  That's exactly what I wanted to hear!
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