Viewstate on a Web User Control

L

Larry

I have a user control on a aspx web page. Both the page and the user control have the "EnableViewState" = True. For some reason, the web user control is not maintaining its state. This same web user control is used on other pages in the same manner, and in these other pages it maintains its viewstate. What can I look for on the problematic page?
Thanks
 
T

Teemu Keiski

Hi,

how are you placing the UC to the Page? Programmatically or declaratively?
Do you do something customized with placing the control to the Page? Post
some sample code to demonstrate.

Thanks,
 
J

Jeffrey Tan[MSFT]

Hi larrya,

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.
Based on my understanding, your page and user control both enabled
viewstate, but when using, your user control's state did not maintain.

===========================
What is the representation of "your user control's state did not maintain"
? After each postback,does your control return to its original state or
anything else?
I think you'd better provide some detailed explaination of your problem, so
we can help you better.

Based on my experience, the viewstate problem may occur under the
environment of dynamic generated control.
If you generate your control at runtime, your control's life time and state
maintain will begin from the point of adding into the Form's
ControlCollection. So each postback, you should manipulate the control
properly.

Anyway, waiting for further more information feedback from you, I will work
with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
N

News Hunter

i have the same problem. as you say i have a dropdown that filled by
dataset. how can i keep its state?
 
J

Jeffrey Tan[MSFT]

Hi News,

As you said, you have the same problem as larrya. Thanks for your
information.

Based on my experience, the problem may occur under the environment of you
use code like this:
private void Page_Load(object sender, System.EventArgs e)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);

DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="job_desc";
DropDownList1.DataValueField="job_id";
DropDownList1.DataBind();
}

private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=DropDownList1.SelectedItem.Text;
}

Whenever you select which item in dropdownlist, if you click button and
postback, you will find Label always show the first item Text.(Also, the
dropdownlist re-initialized with first item being selected.)

=============================================================
I think this is what you and larrya's concern about state maintain.

Because each time when you postback, all the code in Page_Load will
execute. So the DropDownList will retrieve data from dataset and
re-databind again.
So the dropdownlist will return to its original state.

Actually, when page loading, you should determine whether it is the first
time visit and only initialize the dropdownlist at the first time, Iike
this:
private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack==false)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);

DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataTextField="job_desc";
DropDownList1.DataValueField="job_id";
DropDownList1.DataBind();
}
}

============================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If I misunderstand you, please
feel free to tell me in the group. I am standing by to be of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
A

Alvin Bruney

Did you get help on this?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
News Hunter said:
i have the same problem. as you say i have a dropdown that filled by
dataset. how can i keep its state?

--
-----------------------
-News Hunter-
-----------------------
"Jeffrey Tan[MSFT]" said:
Hi larrya,

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.
Based on my understanding, your page and user control both enabled
viewstate, but when using, your user control's state did not maintain.

===========================
What is the representation of "your user control's state did not maintain"
? After each postback,does your control return to its original state or
anything else?
I think you'd better provide some detailed explaination of your problem, so
we can help you better.

Based on my experience, the viewstate problem may occur under the
environment of dynamic generated control.
If you generate your control at runtime, your control's life time and state
maintain will begin from the point of adding into the Form's
ControlCollection. So each postback, you should manipulate the control
properly.

Anyway, waiting for further more information feedback from you, I will work
with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi larrya

Does my reply to you and News Hunter makes sense to you?
If you have any further concern, please feel free to tell me, I will work
with you.Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top