What am i missing to raise a javascript alert

S

simon

hello.
hopefully another quick newbie question for you....

I have a codebehind function coded to handle the click event of a
button. what this function does is insert values from a data grid
into the database. after that is done i want to send the user to
PageC, but before i do that i check a variable value. if that value
is "0" then i want to raise a javascript alert telling the user they
need to set some info in PageA and then automatically send them to
PageA

in the bottom of the function, i have the following....

If PrefCount = 0 Then
Dim strMessage As String = "Your data has not been set yet, you
will be sent to that page to fill it out"
Dim strScript As String = "<script language=JavaScript>alert('" &
strMessage & "');</script>"
If (Not Page.IsClientScriptBlockRegistered("prefCheckScript"))
Then
Page.RegisterClientScriptBlock("prefCheckScript", strScript)
End If
NextButton.Attributes.Add("onClick", "return prefCheckScript();")
Response.Redirect("PageA.aspx")
End If


as i step thru the code i see that everything is firing and working
correctly except that the javascipt pop-up doesn't come up and they
are sent directly to PageA, which is kind of what i want except i want
the popup alert to show and when they click OK, they are then
redirected to PageA.
i also tried using IsStartupScriptRegistered/RegisterStartupScript
first, same thing happens.

adding the onClick attribute to the button doesn't make much sense
here as they have already clicked the button to get into this
function. i put that in just because i saw it on web page out there.

basically trying to figure out how to kick off this javascript
conditionally. don't want it to popup/redirect if my IF condition is
false. but its not firing at all at this point.

thanks for any help!!
 
C

Cor Ligthert [MVP]

Simon,

It is easier to set an extra label on your page that you use for this kind
of information and than use normal server side code. An error should be an
accident so an extra sent is than not that problem in my opinion. Try to
avoid JavaScript if it is not needed (which goes not forever).

Just my thought,

Cor
 
S

simon

hi, thanks for the reply and advice. appreciate it.
even though this may not be the best use of executing javascript from
within a codebehind, it really was more of a functionality question.
in that there will surely be times when this processing is valid, so
i'm really just after what i need to do to execute javascript other
than an "onclick" type of event, something conditionally for example.

so besides these lines to register the javascript
If (Not Page.IsClientScriptBlockRegistered("prefCheckScript"))
Then
Page.RegisterClientScriptBlock("prefCheckScript", strScript)
End If

what do i use to execute the script?

thanks
 
S

simon

hello, in that example, the script is put into a label and basically
run constantly (or immediately) on the page. what i was looking to do
is call the javascript function conditionally....

simple flow would be

IF true
execute javascript
else
do other vb code

thoughts?
 
C

Cor Ligthert [MVP]

Simon,

Your javascript is done at client side
Your vb code is done at serverside (in fact does it create client javascript
code).

Another sample I have is this one. It looks that bad that I don;t set it on
the site, probably does it however more what you are asking.

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String
If Not IsPostBack Then
TextBox1.ForeColor = Color.White
TextBox1.BorderStyle = BorderStyle.None
Dim alertScript As String = _
"<script language=JavaScript>document.all.item('TextBox1').value
" & _
" = prompt('Give me text','My name is nobody'); </script>"
RegisterStartupScript("Startup", alertScript)
Else
str = TextBox1.Text
End If
End Sub
///

And than there is of course AJAX.

http://www.vb-tips.com/default.aspx?ID=12499601-19c8-43bf-94bd-73214b27750c

However that is at the moment (one of) Ken's part of our website.

Cor
 
S

simon

based on the server or client processing, there doesn't seem to be a
way to connect these elements. at least not that i'm seeing.
one thouhgt i had was that if the condition arrises, send them to a
page that pops up the alert like i want and then when they click OK on
the alert, send them to the page i wanted to. make sense? bad form?
 
C

Cor Ligthert [MVP]

Simon,
based on the server or client processing, there doesn't seem to be a
way to connect these elements. at least not that i'm seeing.
one thouhgt i had was that if the condition arrises, send them to a
page that pops up the alert like i want and then when they click OK on
the alert, send them to the page i wanted to. make sense? bad form?

Of course does that makes sense, however just as my first answer a label and
a whatever serverside button (button, linkbutton, imagebutton) can do all
you ask.

And the only thing is to make those controls at the right time visible on
the server side.

Cor
 

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