c# - Javascript confirm message problem -
i have popup confirm box able show below.
but dont know if user clicked ok or cancel.
scriptmanager.registerstartupscript(this, this.gettype(), "ajax", "<script language='javascript'>confirm('do u wanna change?');</script>", false);
so want this.
if (orignalid != newid) { scriptmanager.registerstartupscript(this, this.gettype(), "ajax", "<script language='javascript'>confirm('do u wanna change?');</script>", false); if (user clicks yes) { add data sql } else { return; } }
how know user has clicked??
i have tried this
- i put code below in folder1\jscrip.js file dont kno how call have used ajax update panel in page cannot use clientscript.registerclientscriptinclude reference it. mentioned in 6th point @ link: http://www.dotnetcurry.com/showarticle.aspx?id=274
page.clientscript.registerclientscriptinclude("selective", resolveurl(@"folder1\jscrip.js"));
function confirmation() { if(confirm("are sure?")==true) return true; else return false; }
any suggestions???thanks
functionality:
so user clicks button called "save first" in checks condition "if (orignalid != newid)" if true confirm box shown or else no confirm box shown.. if user clicks ok values enterd in db or else returns , nothing
some code:
protected void page_load(object sender, eventargs e) { if (!ispostback) { } else if (label.text != "") { global.logger.debug("postback happ, label = " + label.text); button2_click(sender, e); } } protected void button2_click(object sender, eventargs e) { if (orignalcsid != 0 && newcsid != 0) { if (orignalid != newid) { global.logger.debug("pop crossed1"); scriptmanager.registerstartupscript(this, this.gettype(), "ajax", string.format(cultureinfo.invariantculture, @"__dopostback('{0}', confirm('your data id1 populated in id2?').tostring());", label.text), true); } else if (page.request["__eventtarget"] == label.text) { global.logger.debug("__eventargument1 = " + page.request["__eventargument"]); bool userclickedok = boolean.parse(page.request["__eventargument"]); if (userclickedok) { // add data sql. } else { return; } label.text = ""; } } }
you use hidden field, still have force postback after user dismisses confirm box. since end calling __dopostback() anyway, can take advantage of second argument post return value of confirm()
server without using hidden field:
if (originalid != newid) { scriptmanager.registerstartupscript(this, gettype(), "ajax", string.format(cultureinfo.invariantculture, @" __dopostback('{0}', confirm('are sure?').tostring()); ", yourupdatepanel.clientid), true); } else if (page.request["__eventtarget"] == yourupdatepanel.clientid) { bool userclickedok = boolean.parse(page.request["__eventargument"]); if (userclickedok) { // add data sql. } else { return; } }
first, compare 2 ids. if they're different, need show confirm box on client , post result server, include startup script (note can pass true
last argument registerstartupscript() in order have generate <script>
tags you).
then, on client side, need call __dopostback()
force postback after blocking call confirm()
returns. __dopostback()
takes 2 arguments: clientid
of control initiated postback (the event target), , optional event argument. they're used internal asp.net plumbing in order raise postback events, doesn't mean can't use them our advantage.
moreover, if postback occurs , updatepanel
event target, aforementioned asp.net plumbing refresh panel (or more, depending on updatemode
properties, that's subject). call __dopostback()
clientid
of updatepanel
containing controls want refresh, , return value of confirm()
, converted string.
back server side, page reloaded, , our code runs again. since implied in question's comments if originalid
, newid
still different, we'll have show confirm box again, assume same time around (the else if
ensures that).
the arguments passed __dopostback()
accessible server-side __eventtarget
, __eventargument
request variables. first, check if event target our updatepanel
, , if that's case, know we're handling forced postback , value of __eventargument
meaningful. call boolean.parse() deserialize string generated earlier on client side, , use decide whether or not update database.
Comments
Post a Comment