page.ispostback question

G

Guest

I have a routine that i only want to execute if a variable is set to Y. The
routine is getting some information on the screen then redirecting to a new
page.
How can i passed a variable to the Page.IsPostBack routine on the page_load
event?

this is what I'm trying to do/
sub page_load
if page.ispostback then
dim value as string = CType(Session("quesiton"), string)
if value = "Y" then
'execute my routine
else
'do my datagrid databinding, etc.
end if
end if

thanks
 
G

Guest

i tried all 3 and its not getting the value of the variable being passed,
thats i posted the question to see if there was another way or a coding error
in my code below.
i need to pass the variable when a button on the page is clicked and only
that time
 
S

Scott M.

Create a label and set its visibility property to false. In the
non-postback code, set the text property of that label to "Y" if your
condition is met. In the postback code, simply check the text property of
the label for "Y".
 
G

Guest

You can pass on the querystring, but not to the current executing page
without Response.Redirect. In your case, you are probably looking at setting
the session var in the page and then testing for Y, right? Session vars are
not set until the session cookie goes out to the user and returns on the next
hit.

The same is true for any other method.

I would need to see more of your model to give you a better suggestion. Your
code, if the value was set prior to this hit, is fine.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
G

Guest

here is what i'm trying to do.
the user can select items on a datalist via a checkbox. They can hit a
button and get redirected to a information page on the items selected by them.
I can get all the items the user selected fine in the page.ispostback
section, but that breaks some other things my page is doing such as refresh.
I want the routine to get the users selection only when the button is hit.

button code:
Public Sub getCars_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles getCars.Click
getSelectedCars ' this gets the users selections then redirects to info
page
'i tried the label thing, and didnt' work
'i tried redirecting to the page itself and passing a querystring and
didn't work
' i tried creating a session varaible and didn't work/
' i'm out of ideas on this one.

End Sub

onload
in my page_load even
if page.ispostback then
getSelectedCars ' this only works here no where else I need to execute
of the value of Y is true
else
'then all my refresh the screen code
else
'do post backs for selection in drop down.
end if
I only need the getSelecetedCar routine execute only if the button is selected
 
D

darrel

i tried all 3 and its not getting the value of the variable being passed,
thats i posted the question to see if there was another way or a coding error
in my code below.
i need to pass the variable when a button on the page is clicked and only
that time

When I need to pass variables like this, I tend to use a redirect:

on button click
execute logic
redirect to 'thispage' + 'query string with variable'

but I'm not sure of the full logic of your page so no idea if that would
really work for you.

-Darrel
 
D

darrel

button code:
Public Sub getCars_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles getCars.Click
getSelectedCars ' this gets the users selections then redirects to info
page

If you are redirecting, then you need to pass the variables with the
redirect. I'd use a querystring and redirect to the URL with the variables
as the querystring. In what way was that not working for you?
in my page_load even
if page.ispostback then
getSelectedCars ' this only works here no where else I need to execute
of the value of Y is true

But you are redirecting on the button click after a person chooses their
cars. Why are you checking for the selected cars here on the postBack?

-Darrel
 
B

bruce barker

when you redirect to a new page, when that page is processed, it will not be
a postback. you ned to the check when it is not a postback


-- bruce (sqlwork.com)
 
D

darrel

i'm doing a post back to get all the items selected from the datalist
control.

You are confusing me. According to the sample code you provided, you're
doing a redirect on the button click. As such, there won't be a postback.

Or maybe I'm confusing myself. I think this is what is happening:

- select cars
- push button (triggers postback)
- check for isPostback and check values
- execute button handler and set values
- redirect

I think you're trying to check the values before you set them.

To get around, this is what I've used in the past:

- select cars
- push button
- if isPostback
- set values
- redirect with querystring
- if it isn't a postback
- check values from querystring

-Darrel
 
G

Guest

this might help here is the code that gets all the checkboxes that are
checked from the user:

this is called when the user clicks the button
Public Sub getSelectedCars()
Dim I As Long
Dim CurrentCheckBox As CheckBox
Dim cbValue As String
Dim gameNum As TextBox
Dim game As String
For I = 0 To dlCars.Items.Count - 1
CurrentCheckBox = dlScores.Items(I).FindControl("chkCars")
carMake = dlScores.Items(I).FindControl("carMake")
cbValue = cbValue & CurrentCheckBox.Checked.ToString()
If CurrentCheckBox.Checked.ToString = True Then
carMake= carMake & carMake.Text & "|"
End If
Next
Response.Redirect("webform2.aspx?cars=" & carMake)
End Sub

this is the page_load event:
page_load
if page.ispostback then
getSelectedCars
end if

if i call getSelectedCars in the is NOT isPostBack then
it returns 0, nothing even if cars are selected
 
G

Guest

maybe this will help:

my page load event
if page.ispostback then
getSelectedCars 'if i put this in the if NOT isPostBack then, it will
return nothing even if items are checked
end if



the routine that gets the checked checkboxes checked by the user when they
click the button.

sub getSelectedCars
Dim I As Long
Dim CurrentCheckBox As CheckBox
Dim cbValue As String
Dim carMake As TextBox
Dim carsAs String
For I = 0 To dlCars.Items.Count - 1
CurrentCheckBox = dlCars.Items(I).FindControl("chkCars")
carMake= dlScores.Items(I).FindControl("carMake")
cbValue = cbValue & CurrentCheckBox.Checked.ToString()
If CurrentCheckBox.Checked.ToString = True Then
carMake= carMake & carMake.Text & "|"
End If
Next
'Response.Redirect("webform2.aspx?cars=" & carMake)
 
D

darrel

my page load event
if page.ispostback then
getSelectedCars 'if i put this in the if NOT isPostBack then, it will
return nothing even if items are checked
end if

That is correct.

You are confused on the order of things and how postbacks work. Once you
redirect, you're clearing all viewstate information for that page. So, your
page is doing this:

- user selects items
- hits the button
- page postbacks ***
- page redirects

you are trying to grab the variables at the *** stage. You can do that, but
you need to do that in the isPostback section and then redirect from there
WITH the variables appended as a queryString (or some other manner).

So I think your solution is to move the redirect from the button click
handler and instead place it in the isPostBack part of the PageLoad.

So it'd be like this:

- user selects items
- hits the button
- page postbacks, sets values, puts them in a querystring, redirects with
querystring

-Darrel
 

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

Top