ASP.NET c# Fix for the OK event of a custom ConfirmMessagebox inside a function library -
we made function shows modalpopupmessage dynamically c# through javascript, works fine wanted add parameter can pass function delegate (or event handler) called if user presses ok button. suggestions?
postdata: don't want typical "confirm want press button" solution function ask confirmation in part of process if necessary. example: user click on delete item button, in codebehind check item has dependency show confirmation message mensaje function passing delegate deleteitemconfirmed(), if user clicks ok call delegate...
function in library:
public static void mensaje(string mensaje, eventhandler eventoclickllamar, bool botoncancelar, string cssclass, color colorfondo) { string colorfondox = colortranslator.tohtml(colorfondo); string idbotonok = eventoclickllamar == null ? "" : eventoclickllamar.method.name.replace("_click", ""); string script = " function verifystyle(selector) {" + " \r\n" + " var rules;" + " \r\n" + " var haverule = false;" + " \r\n" + " " + " \r\n" + " if (typeof document.stylesheets != \"undefined\") { //is supported" + " \r\n" + " var csssheets = document.stylesheets;" + " \r\n" + " " + " \r\n" + " outerloop:" + " \r\n" + " (var = 0; < csssheets.length; i++) {" + " \r\n" + " " + " \r\n" + " //using ie or firefox/standards compliant" + " \r\n" + " rules = (typeof csssheets[i].cssrules != \"undefined\") ? csssheets[i].cssrules : csssheets[i].rules;" + " \r\n" + " " + " \r\n" + " (var j = 0; j < rules.length; j++) {" + " \r\n" + " if (rules[j].selectortext == selector) {" + " \r\n" + " haverule = true;" + " \r\n" + " break outerloop;" + " \r\n" + " }" + " \r\n" + " }//innerloop" + " \r\n" + " " + " \r\n" + " }//outer loop" + " \r\n" + " }//endif" + " \r\n" + " " + " \r\n" + " return haverule;" + " \r\n" + " }//eof" + " \r\n" + " function setfading(o, b, e, d, f) {" + " \r\n" + " var t = setinterval" + " \r\n" + " (" + " \r\n" + " function () {" + " \r\n" + " b = stepfx(b, e, 2);" + " \r\n" + " setopacity(o, b / 100);" + " \r\n" + " if (b == e) {" + " \r\n" + " if (t) { clearinterval(t); t = null; }" + " \r\n" + " if (typeof f == 'function') { f(); }" + " \r\n" + " }" + " \r\n" + " }" + " \r\n" + " , d / 50);" + " \r\n" + " }" + " \r\n" + " function setopacity(e, o) {" + " \r\n" + " // ie" + " \r\n" + " e.style.filter = 'alpha(opacity=' + o * 100 + ')';" + " \r\n" + " // others" + " \r\n" + " e.style.opacity = o;" + " \r\n" + " }" + " \r\n" + " function stepfx(b, e, s) {" + " \r\n" + " return b > e ? b - s > e ? b - s : e : b < e ? b + s < e ? b + s : e : b;" + " \r\n" + " }" + " \r\n" + " // may consider adding frames support" + " \r\n" + " var w = window;" + " \r\n" + " // shortcut document" + " \r\n" + " var d = w.document;" + " \r\n" + " // canvas, window width , window height" + " \r\n" + " var r = d.documentelement;" + " \r\n" + " var ww = w.innerwidth ? w.innerwidth + w.pagexoffset : r.clientwidth + r.scrollleft;" + " \r\n" + " var wh = w.innerheight ? w.innerheight + w.pageyoffset : r.clientheight + r.scrolltop;" + " \r\n" + " // create block element" + " \r\n" + " var b = d.createelement('div');" + " \r\n" + " b.id = 'message';" + " \r\n" + " b.classname = '" + cssclass + "' || '';" + " \r\n" + " b.style.csstext = 'top:-9999px;left:-9999px;position:absolute;white-space:nowrap;z-index: 1001;';" + " \r\n" + " // classname not passed, set defaults" + " \r\n" + " if (!verifystyle(\"." + cssclass + "\")) {" + " \r\n" + " b.style.margin = '0px 0px';" + " \r\n" + " b.style.padding = '8px 8px';" + " \r\n" + " b.style.border = '1px solid #a4bed0';" + " \r\n" + " b.style.backgroundcolor = '#e0ecf1';" + " \r\n" + " }" + " \r\n" + " var bx = d.createelement('div');" + " \r\n" + " bx.style.csstext = 'position: absolute;left:0px;top:0px;width:100%;height:100%;text-align:center;z-index: 1000;background-color: " + //va seguido sin salto colorfondox + ";opacity:0.5;filter:alpha(opacity=50);'" + " \r\n" + " d.body.insertbefore(bx, d.body.firstchild);" + " \r\n" + " d.body.insertbefore(b, d.body.firstchild); " + " \r\n" + " // write html fragment " + " \r\n" + " b.innerhtml = '<table><tr><td>" + mensaje + "</td></tr><tr><td align=\"center\">" + (string.isnullorempty(idbotonok) ? "<input type=\"submit\" value=\"aceptar\" onclick=\"disabled=true;setfading(b, 100, 0, 1000, function () { d.body.removechild(bx); d.body.removechild(b); });\" >" : "<input type=\"submit\" value=\"aceptar\" onclick=\"__dopostback(\\'" + idbotonok + "\\',\\'\\')\" id=\"" + idbotonok + "\" >") + (botoncancelar ? "<input type=\"submit\" value=\"cancelar\" onclick=\"disabled=true;setfading(b, 100, 0, 1000, function () { d.body.removechild(bx); d.body.removechild(b); });\" >" : "") + "</td></tr></table>';" + " \r\n" + " // save width/height before hiding " + " \r\n" + " var bw = b.offsetwidth;" + " \r\n" + " var bh = b.offsetheight;" + " \r\n" + " // hide, move , show" + " \r\n" + " b.style.display = 'none';" + " \r\n" + " b.style.top = (wh / 2 - bh / 2) + 'px'; //center" + " \r\n" + " b.style.left = (ww / 2 - bw / 2) + 'px'; //center" + " \r\n" + " b.style.display = 'block';" + " \r\n"; scriptmanager.registerclientscriptblock((page)httpcontext.current.handler, typeof(page), "mensaje", script, true); } test page:
public partial class test: page { protected void page_load(object sender, eventargs e) { } btndeleteitem_click(object sender, eventargs e) { //db checks .... .... //after x checks against database see item has dependency ask confirmation functionslibrary.mensaje("this item has x dependency, sure want delete it?", btndeleteitemconfirmed_click, true, "cssclassx", system.drawing.color.gray); } btndeleteitemconfirmed_click(object sender, eventargs e) { //delete item definitively, handle dependencies etc... } } options couldnt make work (in case on track wrong):
case 1: (the 1 implemented in our example code) using eventhandler + __dopostback if have buttonid_click defined in page called on postback. (the event didnt rise...we suppose because didnt add ok control in page load...which gives case 2)
case 2: save delegate function passed, register event page.load+=getpostbackcontrolid(), in following page load getpostbackcontrolid() called, there check if control id clicked our ok button, if call delegate function
so want talk codebehind javascript dialog? best solution use ajax. check out how done here: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
remember you'll calling static method in codebehind not data in page available. can though example access session object through httpcurrent object.
good luck.
Comments
Post a Comment