UserControl and sending/getting information from it

C

Cederstrom

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
 
H

Hermit Dave

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.
 
C

Craig Deelsnyder

Cederstrom said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top