473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

UserControl and sending/getting information from it

Hello Group,

I have created an ASP.NET page. The page consist of the following items:
- Button A
- Button B
- UserControl

When I press Button A, I execute the following code:
ViewState["pdbmode"] = "edit";
Session["pdbmode"] = "edit";

When I press Button B, I execute the following code:
ViewState["pdbmode"] = "create";
Session["pdbmode"] = "create";

In my UserControl PageLoad() I have the following code:
if (ViewState["pdbmode"] == null)
Label1.Text = "Null";
else if(ViewState["pdbmode"] == "edit")
Label1.Text = "Edit";
else if(ViewState["pdbmode"] == "create")
Label1.Text = "Create";
else
Label1.Text = "Unknown";

if (Session["pdbmode"] == null)
Label2.Text = "Null";
else if(Session["pdbmode"] == "edit")
Label2.Text = "Edit";
else if(Session["pdbmode"] == "create")
Label2.Text = "Create";
else
Label2.Text = "Unknown";

---

The problem is that when I press Button A, Label1 which uses ViewState never
changes away from being Null.
Label2 Changes, but I it changes too late. I press Button A, Label2 stays
"null", then I press Button A and it changes to edit.

I guess I need to do this completly different, but how?

Basically I just need my UserControl to know if ButtonA was pressed, or
ButtonB was pressed.

I hope someone can help me, please :)
- Cederström
Nov 18 '05 #1
2 1336
The Page Load for a control is always before the control is handed over to
Event execution....

so what is happening is that you are executing a check on your
ViewState["pdbmode"] for null and assigning it always and then

when Button A . click event is called

you assign view state... and session...
ViewState["pdbmode"] = "edit";
Session["pdbmode"] = "edit";

once you are out... the render is called and it renders....

put the check for checking viewstate and session and to assign label values
in a seperate method call....
CheckAndLoad()
{
if (ViewState["pdbmode"] == null)
Label1.Text = "Null";
else if(ViewState["pdbmode"] == "edit")
Label1.Text = "Edit";
else if(ViewState["pdbmode"] == "create")
Label1.Text = "Create";
else
Label1.Text = "Unknown";

if (Session["pdbmode"] == null)
Label2.Text = "Null";
else if(Session["pdbmode"] == "edit")
Label2.Text = "Edit";
else if(Session["pdbmode"] == "create")
Label2.Text = "Create";
else
Label2.Text = "Unknown";
}

on Page load first check to see if its a post back... if not then you call
the method to check view state and session and assign label text
if(!Page.IsPostback)
{
CheckAndLoad();
}

from you Event handler say Button A click
ViewState["pdbmode"] = "edit";
Session["pdbmode"] = "edit";
CheckAndLoad();

assign values... and then call the method to check viewstate and session and
assign the values to you label.

--
Regards,

HD

"Cederstrom" <lasse#Remove#@*This*cederstrom.dk> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
Hello Group,

I have created an ASP.NET page. The page consist of the following items:
- Button A
- Button B
- UserControl

When I press Button A, I execute the following code:
ViewState["pdbmode"] = "edit";
Session["pdbmode"] = "edit";

When I press Button B, I execute the following code:
ViewState["pdbmode"] = "create";
Session["pdbmode"] = "create";

In my UserControl PageLoad() I have the following code:
if (ViewState["pdbmode"] == null)
Label1.Text = "Null";
else if(ViewState["pdbmode"] == "edit")
Label1.Text = "Edit";
else if(ViewState["pdbmode"] == "create")
Label1.Text = "Create";
else
Label1.Text = "Unknown";

if (Session["pdbmode"] == null)
Label2.Text = "Null";
else if(Session["pdbmode"] == "edit")
Label2.Text = "Edit";
else if(Session["pdbmode"] == "create")
Label2.Text = "Create";
else
Label2.Text = "Unknown";

---

The problem is that when I press Button A, Label1 which uses ViewState never changes away from being Null.
Label2 Changes, but I it changes too late. I press Button A, Label2 stays
"null", then I press Button A and it changes to edit.

I guess I need to do this completly different, but how?

Basically I just need my UserControl to know if ButtonA was pressed, or
ButtonB was pressed.

I hope someone can help me, please :)
- Cederström

Nov 18 '05 #2
Cederstrom wrote:
The problem is that when I press Button A, Label1 which uses ViewState never
changes away from being Null.
Label2 Changes, but I it changes too late. I press Button A, Label2 stays
"null", then I press Button A and it changes to edit.

I guess I need to do this completly different, but how?

Basically I just need my UserControl to know if ButtonA was pressed, or
ButtonB was pressed.

I hope someone can help me, please :)
- Cederström


I believe the problem is that the user control has a ViewState
collection of its own, different from the Page's ViewState. To access
the ViewState you are seeking, check

this.Page.ViewState["pdbmode"]

and so on instead. I think that should work.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: james | last post by:
I cannot get my UserControl's browsable properties to show up in the designer properties panel. I have then public virtual bool TestProp { {get return testProp; } set { testProp = value; } } ...
5
by: Markus Wildgruber | last post by:
Hi! We create an inherited user control and add it to a form. When we successfully compile the project and switch to the designer of the form again, the control disappears and we get 2 error...
3
by: ZhangZQ | last post by:
Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl" in an Assembly "UI.dll", now I create an Application project and want to use the "UserControl1" in another AppDomain, ...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
2
by: junOnline | last post by:
Hi, I am developing an asp.net 2.0 application using visual studio 2005 on my computer and I am getting very inconsistent results when I test on my computer versus from other computers. The...
1
by: James | last post by:
CWinFormsDialog in .net 2.0 lets you host a .Net UserControl as a dialog in a managed C++ MDI application. I.e. in the sample: ----------------------------------------------- #include...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
9
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.