ASP.NET c# 修复了函数库中自定义确认消息框的 OK 事件
本文关键字:消息 确认 自定义 事件 OK NET 函数 ASP | 更新日期: 2023-09-27 18:36:25
我们做了一个函数,通过javascript动态显示从c#到javascript的modalpopupmessage,它工作正常,但我们想添加一个参数,以便我们可以传递一个函数委托(或事件处理程序),如果用户按下OK按钮,就会调用该函数委托(或事件处理程序)。有什么建议吗?
Postdata:我们不想要典型的"确认您要按下此按钮"解决方案,而是在必要时在流程的任何部分要求确认的功能。示例:用户单击删除项目按钮,在代码隐藏中检查项目是否有一些依赖关系,因此显示一条确认消息,其中包含传递委托 deleteitemconfirmed() 的 mensaje 函数,如果用户单击确定调用委托...
库中的功能:
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);
}
测试页面:
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...
}
}
我们无法完成的选项(以防我们走上正轨但出了点问题):
情况 1:(在我们的示例代码中实现的那个)使用 EventHandler + __doPostBack 因此,如果您在页面中定义了buttonid_click,它将在回发时调用。(事件没有上升...我们认为这是因为我们没有在页面加载中添加 Ok 控件......这给了我们案例 2)
情况2:保存通过的委托函数,注册事件Page.Load+=GetPostBackControlID(),在下面的页面加载中调用GetPostBackControlID(),在那里我们检查点击的控件ID是否是我们的确定按钮,如果是这样,调用委托函数
所以你想从Javascript对话框与代码隐藏对话吗? 最好的解决方案是使用Ajax。 在这里查看它是如何完成的: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
请记住,您将在代码隐藏中调用静态方法,因此并非页面中的所有数据都可用。 例如,您可以通过 HttpCurrent 对象访问 Session 对象。
祝你好运。
检查依赖关系后,将 btnDeleteItem.OnClick 设为 btnDeleteItem.OnClick +=btnDeleteItemConfirmed_Click
并设置 btnDeleteItem.OnClientClick OnClientClick="return confirm('Are you sure you want to delete this item?');"
愿这对你有帮助