Link to home
Start Free TrialLog in
Avatar of 66chawger
66chawgerFlag for United States of America

asked on

How to pass variable from code behind to user control

I have been looking at several options in the knowledge base, however, have not found something that fits my needs.  In my code behind "myWebControl.ascx.cs" I re-direct to a user control "myUC.cs".  I want to pass a variable to the user control, possibly a session variable.  

I know I can do "HttpContext.Current.Session["ServerName"] = psServerID;" in my code behind, however, don't know if this will be availabe to my UC after the re-direct.  Note, the UC is just a .cs file (public class UserControl2 : System.Windows.Forms.UserControl)
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

All session variables should be available as long as the current session is active, even with redirects.

Bob
Avatar of 66chawger

ASKER

Bob do you have an example?  I attempted a session variable, but could not get it to work, which it should as you stated.
Did you try something like this in the UserControl?

string serverName = Session["ServerName"].ToString();

Bob
So this is to retrieve it in the user control, correct?
To set the Session variable would be like what I have below using HttpContext:

HttpContext.Current.Session["ServerName"] = psServerID

Instead do:

Session("ServerName") = psServerID or
Session.ToString("ServerName") = psServerID  ??
Are you talking about VB.NET, because C# uses brackets, and VB.NET uses parentheses?

    Session["ServerName"] = psServerID;

would work in a page code behind.  ToString is a method for Object that converts the value to a string.

Bob
Bob, yes, C#...sorry!!!!!   and as always end with a ";"....well depending!    I know this is .NET 101, but I swear I tried this and I could not get my variable in the UC.   Let me revisit and I will let you know.
Bob, this is what I am getting when building my the project that contains the UC we are discussing:

The UC is defined as:
public class UserControl2 : System.Windows.Forms.UserControl

The build error I receive is:

UserControl2.cs(40): The name 'Session' does not exist in the class or namespace 'PackageControl.UserControl2'

Am I missing a using statement, or is Session not available for a windows form usercontrol?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Ok, then that was my initial issue... I can still value the session variable in the code behind without the fully qualified name as we discussed before.  so session[blah blah   in code behind and HttpContect.Current.Session in UC, correct?
Another Quality Expert!
Bob, I think I just had it in my head that the user control was being called as part of a re-direct and passing a variable to it.  It was just brain "burp" if that makes any sense!  Thanks again !
Bob, having a strange situation with httpcontext.
As we discussed previously, I used :

public string psServerName = HttpContext.Current.Session["ServerName"].ToString();

in my UC I needed to use the fully qualified name.  What is happening is my UC is not instatiating. If it hits anything using httpcontext, the UC will not display, if I comment it out it displays.  I tried this using httpcontext.current.response.write and same thing, anytime it runs into a statement with httpcontext, the UC will not load?  Any ideas?  All I am trying to do is what we discussed previously and that is retrieving the session variable in my UC.