Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

postback and value inside button method

Status
Not open for further replies.

jn03

Programmer
Jan 16, 2003
45
CA
Hi all,

Could someone help me to find a way to make the flag value which has been set in the btn_click methods to be used right in postback? (please see pseudo code below)
What I want is, when I click btn2, flag is set to be false, so in postback, it will execute:
if flag=false then do something else

However, what I found is: when btn2 is clicked, postback occurs and uses the old value of flag (=true), then it will set flag=false after the postback. As a result, if the next postback occurs, it will take flag as false. All I want is flag=false to be taken right in the first postback before it does a postback. Any hints? Thanks in advance.


Private Sub Page_Load
If Not (Page.IsPostBack) Then
'flag is a static variable: its value
'is retained between postbacks
flag = true
else
if flag = true then
'do something
else
'do something else
end if
end if
end sub

Private Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
flag=true
End Sub

Private Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
flag=false
End Sub
 
It's happening because the Page_Load event occurs first, then it goes to your event handler and resets the flag. So it always uses the previous value of flag variable in PostBack.
 
Yep, that's what I thought. So, is it mission impossible?

Well, I can use Response.Redirect as a workaround, to pass the flag value to the page:

Private Sub btn1_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
Response.Redirect("WebForm.aspx?flag=false", True)
End Sub

But I don't like this solution. It's slow, and if the user keeps clicking on the same button while the page is still loading, it will open another window :(
 
On first load, put the flag in your Not page.ispostback in the page load, then put it into Viewstate. When you change the flag, change its value within the viewstate. That way its set first when the page loads, and you have access to it throughout the lifetime of your page.

D'Arcy
 
I've tried that too, and it resulted in the same thing: still need a second postback for the flag value to be taken effect. What worked was that I didn't to declare flag to be static anymore. Here is the code:

Sub Page_Load
If Not (Page.IsPostBack) Then
flag=true
ViewState("myFlag") = flag
Else
flag= ViewState("myFlag")
End if
end sub

Private Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
flag=true
ViewState("myFlag") = flag
End Sub

Private Sub btn2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn2.Click
flag=false
ViewState("myFlag") = flag
End Sub

 
Sounds a bit bodgit and Bangit as it's basing the event on a string, but I usually utilise the text on the button to ascertain what should be going on if I've got one doing two or more things. Basically, I set the text on the button based on what it's doing, (i.e., "True" or "False"), and simply ascertain the value of the buttons text property in the click event, and handle the event according to that...
<CODE>
private void Button_One_Click(object sender, System.EventArgs e)
{
try
{
if (Button_One.Text == &quot;True&quot;)
{
// Do something here
// and change the Text Value of the button
Button_One.Text == &quot;False&quot;
}
else if (Button_One.Text == &quot;False&quot;)
{
// Do something else here
// and change the Text Value of the button
Button_One.Text == &quot;True&quot;

}
}
catch (Exception)
{
// Handle Exceptions here
}
}
</CODE>

Often helps clarify the action that clicking the button will invoke as well...

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top