Abbiamo creato una funzione che mostra un messaggio modalpopup in modo dinamico da C# a javascript, funziona bene ma volevamo aggiungere un parametro in modo da poter passare una funzione delegato (o gestore di eventi) che verrebbe chiamato se l'utente preme il pulsante OK. Eventuali suggerimenti?ASP.NET C# Correzione per l'evento OK di un ConfirmMessagebox personalizzato all'interno di una libreria di funzioni
Dati posticipati: non vogliamo la tipica soluzione "conferma di voler premere questo pulsante" ma una funzione per chiedere conferma in qualsiasi parte del processo, se necessario. Esempio: l'utente fa clic sul pulsante Elimina elemento, in codebehind si controlla l'elemento ha qualche dipendenza quindi mostra un messaggio di conferma con la funzione mensaje passando il delegato deleteitemconfirmed(), se l'utente fa clic su OK chiama il delegato ...
Funzione in libreria:
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 this supported" + " \r\n" +
" var cssSheets = document.styleSheets;" + " \r\n" +
" " + " \r\n" +
" outerloop:" + " \r\n" +
" for (var i = 0; i < 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" +
" for (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" +
" // for IE" + " \r\n" +
" e.style.filter = 'alpha(opacity=' + o * 100 + ')';" + " \r\n" +
" // for 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" +
" // we may consider adding frames support" + " \r\n" +
" var w = window;" + " \r\n" +
" // shortcut to document" + " \r\n" +
" var d = w.document;" + " \r\n" +
" // canvas, window width and 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 a 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 to it " + " \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 and then 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);
}
pagina di prova:
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 we see the item has some dependency so we ask for confirmation
FunctionsLibrary.Mensaje("This Item has x dependency, are you sure you want to delete it?", btnDeleteItemConfirmed_Click, true, "cssclassx", System.Drawing.Color.Gray);
}
btnDeleteItemConfirmed_Click(object sender, EventArgs e)
{
//delete item definitively, handle dependencies etc...
}
}
opzioni che abbiamo potuto rendere il lavoro (nel caso in cui eravamo in pista, ma qualcosa non andava):
Caso 1: (Quello implementato nel nostro codice di esempio) Utilizzo di EventHandler + __doPostBack in modo che se il file buttonidclick definito nella pagina venga richiamato nel postback. (L'evento non è aumentato ... supponiamo che fosse perché non abbiamo aggiunto il controllo Ok nel caricamento della pagina ... che ci dà il caso 2)
Caso 2: Salva la funzione delegato passata, registro evento Pagina .load + = GetPostBackControlID(), nel carico GetPostBackControlID pagina seguente() viene chiamato, ci siamo controllare se l'ID di controllo cliccato è il nostro pulsante OK, in caso affermativo chiamare la funzione delegata
mi piacerebbe prendere in considerazione il passaggio a Silverlight e fossato che muro orribile orribile di linea JS – Alex
@alex preferiamo usare javascript o ajax da una funzione contenuta piuttosto che utilizzare i tempi di esecuzione – VSP