C# Control Structure with Session

  • Thread starter Thread starter Frank Bishop
  • Start date Start date
F

Frank Bishop

I am having a problem with this control sturcture. I want to hide a
button if the session object is equal to "Staff". I am not getting an
error, the code simply does not work. Help appreciated.
<code>
void Page_Load(object sender, System.EventArgs e)
{
if (Session["WebUser"] == "Staff")
{
//Response.Write (Session["WebUser"]);
Button1.Visible = false;
}

if (!Page.IsPostBack)
{.........
</code>
 
What did the line "Response.Write...." print? Also, try something like:

string savedState = (string)this.Session["WebUser"];
if (savedState == "Staff")
{
// .....
}
 
I can't get it to work. This works:<code>
void Page_Load(object sender, System.EventArgs e)
{
string savedState = (string)this.Session["WebUser"];
if (savedState == "Staff")
{
Response.Write (Session["WebUser"]);
//Button1.Visible = false;
}

if (!Page.IsPostBack)
{...
</code>
So the response.write test line works, but I can't seem to get it to
hide the button for some reason when I use the 2 statements together.
Help appreciated.
 
I can't get it to work. This works:<code>
void Page_Load(object sender, System.EventArgs e)
{
string savedState = (string)this.Session["WebUser"];
if (savedState == "Staff")
{
Response.Write (Session["WebUser"]);
//Button1.Visible = false;
}

if (!Page.IsPostBack)
{...
</code>
So the response.write test line works, but I can't seem to get it to
hide the button for some reason when I use the 2 statements together.
Help appreciated.

You know, I don't like the == used with a string. Just doesn't feel
right.

Have you tried something like:

if (this.Session["WebUser"].ToString().CompareTo("Staff") == 0)


-- ipgrunt
 
Sorry, you are right. I ran into a case issue too. So I covered that
like this:<code>

void Page_Load(object sender, System.EventArgs e)
{
string savedState1 = Session["WebUser"].ToString();
string savedState2 = savedState1.ToLower();
if (savedState2 == "staff")
{
Button1.Visible = false;
//Response.Write (Session["WebUser"]);
}</code>
Thanks for all the help.
Frank
 
You should use String.Compare(s1, s2, true) and check if it equals 0.

Also, in a previous post you said it _did_ do the response write, but
not hide the button. If that really was true, then how would
case-sensitivity solve anything?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top