me me read me plz ...

G

Guest

Hi ALL,

I have a javascript code which i register using
page.RegisterClientScriptBlock() in my btnFinish() button_click event.

I noticed that the debugger will run thru the whole event
for my button, before launching the javascript
code. I would like to put some c# code in the event if the javascript alert
= true, as well to do some processing. e.g.
If I put an alert javascript code ....

private void btnFinish(system....., event e ....) {

string NoAccess = " <script language='javascript'>
bool a = (alert( " Click OK to Finish, Cancel to return"))
if (a = true) then
document.getElementById("NoAccessAllowed").click();
else
return false;
</script> "
Page.RegisterClientScriptBlock("NoAccess ", NoAccess )

string strSQL = "insert into db_Student (x, y) values (x, y)"
blah blah...
}

HTML codebehind:
<a href="default.aspx" id="NoAccessAllowed" target="_parent"
type="hidden"></a>

Q: Will this C# code insert into the db table only if alert a = true ? or
will it do that everytime this event is triggered ?

TIA. cheers.
 
S

S. Justin Gengo

Andrew,

You're attempting to use javascript the wrong way here (at least in the
example you've given). Javascript only fires (and can be used) on the client
machine. The code on your server will never know what happens on the client
(unless you're javascript writes to a hidden field or a cookie that can then
be retrieved by the server).

But what you're trying to do is much easier than that. Just attach your
javascript alert directly to the button when the page is first generated.
Put the following code into your page load and see what happens.

MyButton.Attributes.Add("onclick", "javascript: alert( 'Click OK to Finish,
Cancel to return');")

Now, it looks as if you are trying to do something else here also. But I
don't know what you intend to do on the Click of the "NoAccessAllowed"
control. If you're trying to return to the same page there is no need. The
script above does that. If you're trying to go to a different page, well, if
NoAccessAllowed is doing a post back it is defeating your purpose of the
javascript above. Let me know what NoAccessAllowed is supposed to do and
I'll see if I can help you out.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
K

Karl Seguin

Andrew:
You need to understand the nature of web programming. You can't go between
server-side and client-side as you please. When a page is requested, first
server-side code fires, and the HTML is rendered to the browser. Any
client-side code you have will then be triggered. Not until a new request
is made (such as a button being clicked and firing postback) will the entire
process restart. When the button is clicked, the page is posted back
(essentially re-requested) and the event handler fires. At this point you
are in server-side, you can't break while some client-side code fires.

You could make your button output the javascript, then have another button
repost to handle the SQL insert...but that's 3 requests (initial,
confirmation, save) which is (a) slow and (b) unituiative for users.

Normally, you attach the javascript to the initial response. So, in page
load, do:

btnFinish.Attributes.Add("onClick", "Check();");

in your html do:
<script language="JavaScript">
function Check()
{
if (confirm("Ok?"))
{
return true;
}
_doPostBack('btnNoAccess','' );
}


Anyways the above code is just dummy code, won't necessary work, but will
hopefully give you some ideas...

Karl
 
G

Guest

Hi,

Thanks for your reply.
I have done the javascript code and it works fine.
I got the alert working. e.g. alert("OK to end the test, Cancel to return");
What I wanted to do was that when the user clicks the OK button of the popup
alert, I will update a record in the database.

However it looks like I can't do that coz the server-side code and
client-side code doesn't relate to each other. How do you suggest I implement
this update ?

Am I being clear enough ? TIA.
Cheers.
 
K

Karl Seguin

Well, if they click ok, the server-side vent has to fire.

the code might need to change to be
btnFinish.Attributes.Add("onClick", "return Check();");

(note the extra return)

if Check() returns true, the code should be posting back and the btnFinish
event should be firing like it used to...

Karl
 
G

Guest

Hi,

I have done what code you written down for me.

I put the btnEndAss.Attributes.Add("onClick", "return EndAss();"); line in
the page_load. I noticed that after I clicked YES on the alertbox, the
debugger loops back to the c# code but doesn't go into the !Page.IsPostBack.

When I click on NO nothing happens, back to the screen again, which is great.

FYO, my html code looks like this:
<script language="javascript">
function EndAss() {
var a = (confirm("Are you sure you want to END this Assessment?" ) )
if (a == true)
{
return true;
//document.getElementById("alink").click();
}
else
{
return false;
}
_doPostBack('btnEndAss','' );
}
</script>

Because this is an asp.net button (btnEndAss), I have an event
i.e. private void btnEndAss_Click(object sender, System.EventArgs e), but
this event is never called at all when I click on the button. Why is that ?

What does _doPostBack('btnNoAccess','' ); do and mean ? It doesn't seem to
be doing anything at all. Also is it supposed to be double-quotes instead of
single-quotes ? How I wish VS.Net is able to debug javascript as well...

From what I understand from your explanation, the btnEndAss event will be
called by the _doPostBack('btnEndAss','' ) line, and I was hoping that I
could put the update table code in here, sigh ... Am I missing something ?

TIA.
Andrew.
 
K

Karl Seguin

you shouldn't go in the !Page.IsPostBack the page is posting back...so
IsPostBack is true

__doPostBack('btnEndAss', ''); should simular the btnEndAss as being
clicked; however, if you look at your JavaScript, you'll see that that code
is never reached. You are returning true in the if, and false in the else.
In other words, whether the user hits yes or no, you are returning (ie,
leaving the function) before you ever get to the line __doPostBack...

I'm not 100% sure what you are doing, but it should look osmething like:

if (a == true)
{
return true;
}
__doPostBack('btnEndAss', '');


or something

Karl
 
G

Guest

Hi Karl,

I have even tried:
e.g.
var a = (confirm("Are you sure you want to END this Assessment?" ) )
if (a == true)
{
//return true;
_doPostBack('btnEndAss','' );
}

eg.
if (a == true)
{
//return true;
}
_doPostBack('btnEndAss','' );

eg.
var a = (confirm("Are you sure you want to END this Assessment?" ) )
_doPostBack("btnEndAss_Click","" );

Even this line doesn't seem to do anything.
The btnEndAss event doesn't seem to be called at all.

I detect that there is a javascript error, but the page posts back too fast
for me to detect the javascript error. Anyway of capturing and displaying
that javascript error ?

Cheers.
Andrew.
 
K

Karl Seguin

There two underscores before doPostBack, not one. My first post had this
error, but all subsequent ones used __ I believe

Karl
 
G

Guest

Hi,

Just wanted to confirm, the line:
__doPostBack('btnEndAss','' );
the btnEndAss is the button click event handler in c# right ?
coz I have a line
private void btnEndAss_Click(object sender, System.EventArgs e)
as the event handler for that button.
That javascript line will cause that button event to be triggered right ?

TIA.
cheers.
 
K

Karl Seguin

btnEndAss is the id of the button, not the event handler. that might only
work with linkbuttons and such, if it's an actual button then

document.getElementById("btnEndAss").click();

might do the trick.

karl
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top