是从服务器端在asp.net中没有对话框 [英] YesNo dialog in asp.net from server side

查看:110
本文介绍了是从服务器端在asp.net中没有对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我在asp.net c#中有代码,我需要在服务器端调用YesNo对话框。实际上,代码将检查数据库,并且在某些条件下,它会询问用户是否要继续?现在,我有一些函数或java脚本,但都在客户端而不是服务器端。你能否建议一下这样做的方法。感谢您的帮助与合作。



BR //

Dear all,

I have code in asp.net c# where I need to call the YesNo dialog in server side. Actually, the code will check the database and for certain condition it will ask the user does he want to continue or not? Now, I have got some functions or java scripts but all are in client side not server side. Can you please suggest the way to perform so. Thanks for your always help and cooperation.

BR//

推荐答案

如果你没有得到它但是,您永远不会调用服务器端的任何UI相关元素。你总是在客户端做这件事。服务器端只能在HTTP响应中生成HTML文本或其中的一部分(或其他资源),但UI本身始终在浏览器中运行。当用户在浏览器中执行某些操作时会发生什么,例如,选择是或否,取决于处理此事件的目的。有时一切都发生在客户端。其他所有内容都是通过另一个HTTP请求执行的。此请求可以请求相同或新的URI,或通过Web表单发送请求(也可以使用一些或另一个称为action的URI),或者重要的是,发送Ajax HTTP请求。假设您在服务器端执行检查数据库(这是最合理的方式),您需要以下列方式之一生成另一个HTTP请求。



因此,我们回到客户端(!)UI,从而回到JavaScript。请求确认的最简单方法是调用 confirm

If you did not get it yet, you never "call" any UI-related element on server side. You always do it on client side. Server side can only generate HTML text or part of it (or other resources) in HTTP response, but the UI itself always operates in the browser. What happens when the user do some action in browser, say, chooses Yes or No, depends on the purpose of handling this event. Sometimes everything happens on the client side. Everything else is performed through another HTTP request, always. This request could request the same or new URI, or send a request via a Web form (also with some or another URI called "action") or, importantly, Ajax HTTP requests. Assuming your "check the database" is performed on the server side (which is the most reasonable way), you need to generate another HTTP request in one of these ways.

So, we are getting back to client-side (!) UI and hence JavaScript. The simplest way to request confirmation is calling confirm:
var confirmation = confirm("Do you want to continue?");
if (confirmation) sendSomeHttpRequest(/* ... */);



唯一的问题是,对话框不会显示是/否,而是确定/取消(上面显示的调用的if分支将在按下OK后执行),在大多数情况下可能没问题。



更严重功能和灵活性,你可以使用jQuery Dialog (强烈推荐)。您可以在此处查看其工作原理: http://jqueryui.com/dialog [ ^ ]。



但我想补充一下:通常要求用户输入内容到目前为止,不是最好的设计。但建议替代方案需要更详细地解决问题。



-SA


The only problem, the dialog will show not "Yes"/"No", but "OK"/"Cancel" (the "if" branch with the call shown above will execute after the press on "OK"), which could be fine in most cases.

For more serious features and flexibility, you can use jQuery Dialog (highly recommended). You can see how it works here: http://jqueryui.com/dialog[^].

But I would add: requesting a user for something is usually not the best design, by far. But suggesting the alternatives need going into much finer detail of the problem.

—SA


这是另一个解决方案,它的工作原理



Here is another solution and it works

//code similar to following need on button click
if (some conditional expression is true)
{
                    mpeConfirmation.Show();  //please make note of this
                    return;
}

//Following I have defined in Markup and gets displayed from server-side as mentioned in the code snippet above

<asp:Panel ID="pnlConfirmation" runat="server" Width="600px"  Style="display: none" EnableViewState="true">
            <div runat="server" id="divConfirmation" class="ModalPopupHeader">
    Confirm?
            </div>
            <uc:Confirmation ID="ucConfirm" runat="server" />
        </asp:Panel>
        <asp:Button ID="btnDummyConfirmation" runat="server" Style="display: none;" />
        <ajaxToolkit:ModalPopupExtender ID="mpeConfirmation" runat="server" BehaviorID="mpeConfirmationBehavior"

            TargetControlID="btnDummyConfirmation" RepositionMode="None" PopupControlID="pnlConfirmation"

            PopupDragHandleControlID="divConfirmation" DropShadow="true" BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>





ucConfirm是使用所需布局和按钮创建的UserControl。 '是','不'我在此控件中定义了两个事件,如下所示





ucConfirm is a UserControl created with required layout and buttons. 'Yes', 'No' I have defined two events inside this control like follows

  /// <summary>
       /// Event for On OK button clicked
       /// </summary>
       public event EventHandler OKClicked;

       /// <summary>
       /// Event for On Cancel button clicked
       /// </summary>
       public event EventHandler CancelClicked;


private void OnOKClicked(object sender, EventArgs e)
       {
           if (OKClicked != null)
           {
               OKClicked(sender, e);
           }
       }

       private void OnCancelClicked(object sender, EventArgs e)
       {
           if (CancelClicked != null)
           {
               CancelClicked(sender, e);
           }
       }

       protected void btnOk_Click(object sender, EventArgs e)
       {
           OnOKClicked(sender, e);
       }

       protected void btnCancel_Click(object sender, EventArgs e)
       {
           OnCancelClicked(sender, e);
       }



我在用户控件中消耗了这些事件,无论我需要从服务器端启动的确认消息功能,而不是按钮客户点击。因为我不想要确认总是显示,但只有在某些条件下我采取这种方法





希望有帮助

谢谢


I've consumed these events in the user control wherever I needed the Confirmation message functionality initiated from Server side, rather than on button client-click. Since I did not want the confirmation to show always, but only on certain condition I took this approach


Hope that helps
Thanks


解决方案1很清楚,我在这里有另一种选择





Ajax confirmbuttonextender
[ ^ ]





http://www.ajaxtutorials.com/asp-net/ajax-control-toolkit-confirmbutton-extender-tutorial-in-asp- net-4-0-c / [ ^ ]
Solution 1 is well clear, and i have another option here


Ajax confirmbuttonextender
[^]


http://www.ajaxtutorials.com/asp-net/ajax-control-toolkit-confirmbutton-extender-tutorial-in-asp-net-4-0-c/[^]


这篇关于是从服务器端在asp.net中没有对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆