ViewState button event....stumped

  • Thread starter Thread starter James Glover
  • Start date Start date
J

James Glover

I need help...

I am trying to set the Viewstate of a var on an onclick event and let the
page post back to get the new value. When i click the button, the first time
though (on the post back) it reads empty string. On subsequent clicks it
seems to be on off the actual value I have assigned to the ViewState
variable.

Some psudio code below:

btnPush = (string)ViewState["btnPush"];
topicCount = (int)ViewState["topicCount"];

if (btnPush == "")
{
curNum = Convert.ToInt32(Request.QueryString["CurNum"]);
topicID = Convert.ToInt32(Request.QueryString["TopicID"]);
}//end if (btnPush.Length == 0)

if (btnPush == "Next")
{
curNum = Convert.ToInt32(ViewState["CurNum"]) + 1;
topicID = Convert.ToInt32(ViewState["NTopID"]);
}//end if (btnPush == "Next")

if (btnPush == "Prev")
{
curNum = Convert.ToInt32(ViewState["CurNum"]) - 1;
topicID = Convert.ToInt32(ViewState["PTopID"]);
}//end if (btnPush == "Prev")

private void NextButton_Click(object sender, System.EventArgs e)
{
ViewState["btnPush"] = "Next";
ViewState["TopicID"] = Convert.ToInt32(ViewState["PTopID"]);
}



The fiirst time i click the Next button the variable btnPush still reads as
"". The second time it reads the button value I pushed previously, etc. I
seems to always be one behind the actual value set on the click event. Any
ideas? I am totally lost on this one.

James
 
James:

I am not sure where (what event) you are trying to read the ViewState value,
but for informational purposes the Page Init and Load events occur before
the control postback event procedures. This is one possible explanation for
why you are one step behind in the value.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I need help...

I am trying to set the Viewstate of a var on an onclick event and let the
page post back to get the new value. When i click the button, the first time
though (on the post back) it reads empty string. On subsequent clicks it
seems to be on off the actual value I have assigned to the ViewState
variable.

Some psudio code below:

btnPush = (string)ViewState["btnPush"];
topicCount = (int)ViewState["topicCount"];

if (btnPush == "")
{
curNum = Convert.ToInt32(Request.QueryString["CurNum"]);
topicID = Convert.ToInt32(Request.QueryString["TopicID"]);
}//end if (btnPush.Length == 0)

if (btnPush == "Next")
{
curNum = Convert.ToInt32(ViewState["CurNum"]) + 1;
topicID = Convert.ToInt32(ViewState["NTopID"]);
}//end if (btnPush == "Next")

if (btnPush == "Prev")
{
curNum = Convert.ToInt32(ViewState["CurNum"]) - 1;
topicID = Convert.ToInt32(ViewState["PTopID"]);
}//end if (btnPush == "Prev")

private void NextButton_Click(object sender, System.EventArgs e)
{
ViewState["btnPush"] = "Next";
ViewState["TopicID"] = Convert.ToInt32(ViewState["PTopID"]);
}



The fiirst time i click the Next button the variable btnPush still reads as
"". The second time it reads the button value I pushed previously, etc. I
seems to always be one behind the actual value set on the click event. Any
ideas? I am totally lost on this one.

James
 
Ahh, that's right. Thank you. I think it was a case of over thinking the
code. I have it fixed now and I thank you again.
 

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

Back
Top