简体   繁体   English

想要使用javascript显示消息框

[英]Want to show a message box using javascript

Hi everyone I have a web form in which I am having a button on clicking which data back up is being taken, I used the following javascript : 大家好,我有一个网页表单,其中我有一个按钮点击正在采取备份的数据,我使用以下javascript:

<script language="javascript" type="text/javascript">
   function showPleaseWait() {
       document.getElementById('PleaseWait').style.display = 'block';
   }
</script>

<asp:Button ID="btnTakebackup" runat="server" Text="Take Backup"  Enabled="true" 
   onMouseDown="showPleaseWait()" CausesValidation="false" />

<div id="PleaseWait" style="display: none;">"Please Wait Backup in Progress.."</div>

Hi I am using a button to take a back up. 嗨,我正在使用一个按钮进行备份。

Now I want to show a message in btnTakebackup_Click() event, whether Back up was successful or not. 现在我想在btnTakebackup_Click()事件中显示一条消息,无论备份是否成功。 I used Response.Write("<script>alert('abcd');</script>"); 我使用了Response.Write("<script>alert('abcd');</script>"); in btnTakebackup_Click() event. btnTakebackup_Click()事件中。 But the problem is that I want to show the page also, which is not showing instead white background is showing. 但问题是我想显示页面,而不显示白色背景显示。

Thanks in advance... 提前致谢...

Do you really want it to be an alert? 你真的希望它成为警报吗? (You should know that they lock up the whole browser not just the tab your page is on), do your users really need to acknowledge the backup success by clicking ok or just be informed of it?... (您应该知道他们锁定整个浏览器而不仅仅是您的页面所在的选项卡),您的用户是否真的需要通过单击确定或仅被告知它来确认备份成功?...

I suggest you have a div on the page that says "Backup successful". 我建议你在页面上有一个div,上面写着“备份成功”。 The visibility of which can be set by a boolean property BackUpSuccess which you can set to true in the code behind you mention. 可以通过布尔属性BackUpSuccess设置其可见性,您可以在后面提到的代码中将其设置为true。

<div id="backUpSuccess" <%=BackUpSuccess ? "" : "style='display:none;'"%>>
    Backup was successfull
</div>

...you can style the div as you like in your .css file to get attention. ...您可以在.css文件中根据需要设置div的样式以引起注意。


If you really do want an alert you could run some JavaScript on page load to check the content of a hidden input that you set server side in similar fashion...but running javascript on page load is tricky...unless your using jQuery and then you will know it's very easy. 如果你真的想要一个提醒你可以在页面加载时运行一些JavaScript来检查你以类似的方式设置服务器端的隐藏输入的内容......但是在页面加载上运行javascript很棘手......除非你使用jQuery和然后你会知道这很容易。

To show a message box alert should be able to write out a new script to the response stream: 要显示消息框警报,应该能够将新脚本写入响应流:

var script = 
    "<script type=\"javascript\">" +
    "alert(\"Backup in progress, don't go!\");" +
    "</script>"
Response.Write(script);

However much this is distasteful, I suppose it is sometimes "necessary". 无论多么令人反感,我想它有时是“必要的”。

You can add client side event handlers to ASP controls: 您可以将客户端事件处理程序添加到ASP控件:

How to: Add Client Script Events to ASP.NET Web Server Controls 如何:将客户端脚本事件添加到ASP.NET Web服务器控件

Cheers. 干杯。

From your question, I understood that after clicking on the button, the data back up is happening, but the alert is not displaying as soon as you clicked the button.This is because you are calling the JavaScript in the button click event which will be fired only after all the code in the button click is executed.I suggest you to add a JavaScript function in the .aspx source page it self and call the JavaScript function as shown below: 根据您的问题,我了解到点击按钮后,数据备份正在进行,但是一旦您点击按钮,警报就不会显示。这是因为您在按钮点击事件中调用JavaScript只有在执行按钮单击中的所有代码后才会触发。我建议您在.aspx源页面中添加一个JavaScript函数,并调用JavaScript函数,如下所示:

        <script ...>
        function xyz()
        {
        alert('Please Wait');
        }
        </script>

    and in button declaration

        <asp:button id='btn_submit' runat="server" OnClientClick="return xyz();" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM