Solved

How to pass variable from code behind to user control

Posted on 2007-12-04
12
997 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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