Solved

How to pass variable from code behind to user control

Posted on 2007-12-04
12
991 Views
Last Modified: 2013-12-16
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
Comment
Question by:66chawger
  • 8
  • 4
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20412248
All session variables should be available as long as the current session is active, even with redirects.

Bob
0
 

Author Comment

by:66chawger
ID: 20412840
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20412859
Did you try something like this in the UserControl?

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

Bob
0
 

Author Comment

by:66chawger
ID: 20412944
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20413055
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
 

Author Comment

by:66chawger
ID: 20413335
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:66chawger
ID: 20414012
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 20414073
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
 

Author Comment

by:66chawger
ID: 20414762
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
 

Author Closing Comment

by:66chawger
ID: 31412720
Another Quality Expert!
0
 

Author Comment

by:66chawger
ID: 20415385
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
 

Author Comment

by:66chawger
ID: 20443277
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now