• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

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)
0
66chawger
Asked:
66chawger
  • 8
  • 4
1 Solution
 
Bob LearnedCommented:
All session variables should be available as long as the current session is active, even with redirects.

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

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

Bob
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
66chawgerAuthor Commented:
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  ??
0
 
Bob LearnedCommented:
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
0
 
66chawgerAuthor Commented:
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.
0
 
66chawgerAuthor Commented:
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?
0
 
Bob LearnedCommented:
If you are anywhere where it doesn't recognize Session, Response, Server, etc., then you need the fully-qualified name:

HttpContext.Current.Session

Bob
0
 
66chawgerAuthor Commented:
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?
0
 
66chawgerAuthor Commented:
Another Quality Expert!
0
 
66chawgerAuthor Commented:
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 !
0
 
66chawgerAuthor Commented:
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.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now