Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pass variable from code behind to user control

Posted on 2007-12-04
12
Medium Priority
?
1,001 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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