在 ASP.NET 中返回消息框

赛迪克

我需要一个非常简单的 ASP.NET 代码。

在页面上包含一组语句代码的任何按钮内,都会要求用户确认继续。

如果他同意,它会完成这些订单或停止。

拜托,你能帮帮我吗?

if (ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)
{
    Label1.Text = "ok";
    // other statements
}
else
{
    Label1.Text = "no";
    // other statements
}
阿尔伯特·D·卡拉勒

你不能在网络上做那种代码。请记住,仅当用户单击按钮时才运行背后的代码,然后整个网页都会到达服务器。代码隐藏运行,然后整个页面返回客户端浏览器。页面加载、呈现,然后甚至 JavaScript 代码开始运行。在服务器端,网页已卸载 - 被吹得一塌糊涂。Web 服务器现在正在等待发布任何网页 - 不需要您的页面!

因此,后面的代码永远不会也将直接与用户交互。ONLY 背后的代码可以在很短的时间内触摸、修改服务器上的网页。

如果你说这样做:

TextBox1.Text = "Hello how are you?";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)

或这个:

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)
TextBox1.Text = "hello world";

效果是一样的。

会发生什么:

在此处输入图像描述

所以,现在网页传送到服务器。

您的代码可以修改该文本框。然后注入 JavaScript 块。

你得到这个:

在此处输入图像描述

后面的代码运行。它将注入 JavaScript,运行代码来设置文本框。

此时,我先设置文本框,还是注入 JavaScript 有什么关系?该页面在网络服务器上,只是坐在他们的。用户看到该网络“等待”微调器。

现在,当你的代码完成后,(用户仍然没有看到任何更改),然后整个页面被发送回客户端,如下所示:

在此处输入图像描述

Then whole page re-loads, then JavaScript starts running again, and our injected block of code will NOW run!!!

So, as you can see with above, we don't really interact with the user directly - and in fact we can't!!! - the web page travels up to server - your code behind runs and has a very short little window of time to modify the page, and then the WHOLE page is send back to client, page is re-plot, re-load, and then JavaScript runs again.

So, you not able to "wait", since any code that waits will result in the web page sitting stuck up on the server side.

So in above, the order of the two commands don't matter much, does it? I can modify the text box, or inject some script - but NONE of that will run nor be seen by end user until such time the WHOLE page travels back down to the client side.

So, Any changes to the page, and even in different order in most cases does not matter. Your code is making changes to the web page BEFORE the WHOLE page will be transmitted back to the client side. So, in most cases, the order of things and controls you change on a page don't matter, since those changes can't be seen by the end user until ALL OF your code behind is done, and then the whole page starts the trip back to the client side.

So, in effect, grasping this concept of the round trip is really quite much the most fundamental concept you need to always have clear in your mind to write web code with asp.net. You really can't do web development without the above.

So, we can however add to the button, and use a confirm() like this:

our plane jane asp.net button?

It will now have two routines!!!

It will have the client side click event. This part does the dialog or prompt. If the user answers yes, then we allow the button click (server side) to run. But remember such code can only run client side - that means the WHOLE page has to be sitting on the users desktop - no code behind running at that point.

So, in the most simple format, say we have a button to delete a record.

But, a simple click on the button - rather dangerous.

So, you can add a code stub (JavaScript) to the button like this:

     <asp:Button ID="cmdDelete" runat="server" Text="Server delete prompt" 
            OnClientClick="return confirm('Really delete this file?');"  />

So, when we click this button, you get a JavaScript prompt. And if it returns "true", then the button code behind REALLY does run.

So unlike desktop code, you can have that "old way" of say a if/then block of code, and based on a user prompt, conditional run that code. (because code behind ONLY runs during that so called post-back (round trip).

so, the above will work just fine, and looks like this:

在此处输入图像描述

Now, of course the "main" issue is that the browser built-in dialog prompts do look rather ugly.

Using nice dialog boxes?

Of course, with the exception of "alert()" and "confirm() in JavaScript which HALT code in the browser? In most cases this is not allowed anymore - since it can lock up and freeze the browser.

And thus, now , Almost EVERY new nice looking "add-in"? Code as a general rule in JavaScript does NOT wait anymore - it runs asynchronous.

So, say we using jQuery.UI and we want to dump the VERY ugly built in browser alert() or confirm dialog?

You can do it this way:

        <asp:Button ID="cmdTest" runat="server" Text="Server Delete Prompt" ClientIDMode="Static" style="display:none"
            OnClientClick="return mytest(this)"/>
        <br />

        <div id="MyDialog" style="display:none">
            <h2>my cool dialog text</h2>
            <h2>This delete operation can't be undone</h2>
        </div>
    </div>


    <script>

        var mytest2ok = false
        function mytest(cmdBtn) {
            if (mytest2ok) {
                return true
            }

            myDialog = $("#MyDialog")
            myDialog.dialog({
                title: "Delete the whole server system",
                modal: true,
                appendTo: "form",
                autoOpen: false,
                buttons: {
                    ok: function () {
                        myDialog.dialog('close')
                        mytest2ok = true
                        $(cmdBtn).click()
                    },
                    cancel: function () {
                        myDialog.dialog('close')
                    }
                }
            })

            myDialog.dialog('open')
            return false
        }

    </script>

In this case, we again call and setup a OnClientClick. But, jQuery.UI code as I noted does not wait. So we click on button, our dialog displays and the JavaScript code runs through and finished!!! - that's why we added the ok flag.

So, the dialog is displayed. User hits ok, and then we run the "ok" code stub, and it sets our flag, and clicks the button again!!! - this time the client side button code returns true and the server side button click will run. This code looks like this, and allows you a nice looking dialog, and in effect the SAME reuslts - a pop dialog to conditional run or not the code based on user input.

jQuery.UI lets you use the content of a "div" for anything you want in the dialog.

So, we now have this:

在此处输入图像描述

So, this is a web based dialog box - it even makes the screen go "gray-darker" for you. And it is model. But, the code is not frozen, and when you click ok, then the "ok" stub runs, and clicks the server side button for you again.

So, in most cases, you have to:

Pop the dialog in the client browser, and THEN choose to click or run the server side button code if you want.

Could you do this 100% server side? well, you probably could if you used TWO buttons, and hide the 2nd button. So, you click on first button. Server side code runs and injects the JavaScript to pop dialog, and then based on yes/no, then the 2nd button will be clicked on and your server side code stub will run. But NOTE VERY close, this would suggest and mean you again NEVER are waiting in the middle of your code stub server side, but going to pop a dialog, and then based on user answer, another button code will run server side.

As noted, it thus an advantage to put the dialog box in FRONT of the server side button by using client side code to determine the yes/no choice.

因此,根据用户输入,您要么单击(运行)按钮服务器代码,要么不单击。但是没有一种实用的方法可以在代码存根中间“停止”服务器端代码以等待提示,因为参考上图,您可以在服务器上没有任何用户交互时看到网页,但是您的代码只能在返回客户端之前修改网页。

因此,几乎总是,我们需要一个客户端代码位,然后我们选择是/否来运行该按钮。

因此,后面的代码永远不会与用户交互,而只能等待整个网页被发送到服务器(以便后面的代码运行)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章