简体   繁体   English

JavaScript确认对话框-ASP.NET

[英]Javascript confirmation dialog - ASP.NET

I saw a couple of similar questions on the same topic, but none of them addressed my issue. 我在同一主题上看到了几个类似的问题,但都没有解决我的问题。

I've got a asp.net website, and want to show a status message (asp:label that will disappear in 5 seconds) to the user after the database was updated. 我有一个asp.net网站,并希望在数据库更新后向用户显示状态消息(asp:label将在5秒钟内消失)。

I want to assign the text to the label, and then hide it with Javascript. 我想将文本分配给标签,然后用Javascript隐藏它。

I got the js part sorted out, the only problem is how do I call js function after I assigned the text to the control? 我整理了js部分,唯一的问题是将文本分配给控件后如何调用js函数?

let's say i'm updating something in the database with following code: 假设我正在使用以下代码更新数据库中的内容:

<asp:Button ID="btnUploadFiles" runat="server" OnClick="buttonSubmit_Click" Text="Update"  />

Code behind 后面的代码

protected void buttonSubmit_Click(object sender, EventArgs e)
    { try{// update the database  
          // change label text to tell the user update succeeded}
      catch(Exception ex){...}
    }

please help! 请帮忙!

UPDATE: no jquery please, just plain javascript 更新:请没有jQuery,只是普通的javascript

I'd personally use jQuery for this, but if you want to use plain old JavaScript then something like this will probably do the trick: 我个人使用jQuery ,但是如果您想使用普通的旧JavaScript,那么类似的方法可能会解决问题:

<script type="text/javascript">
function hideLabel()
{
    // replace yourLabelID with <%=YourLabelID.ClientID%> if it's a .NET Label control
    document.getElementById('yourLabelID').style.display = 'none';
}
setTimeout('hideLabel()', 5000);
</script>

If necessary you could also embed the script block in a Literal control and only make it visible when you update your label text. 如有必要,您还可以将脚本块嵌入到Literal控件中,并仅在更新标签文本时使其可见。

To answer your question "How do i call js function after i assigned the text to the control?". 回答您的问题“将文本分配给控件后,如何调用js函数?”。 You can just add a call to 'RegisterClientScriptBlock' inside of your button click event to output the JavaScript provided by Luke. 您可以在按钮单击事件内部添加对“ RegisterClientScriptBlock”的调用,以输出Luke提供的JavaScript。

protected void buttonSubmit_Click(object sender, EventArgs e)
{ 
    try
    {
       // update the database  
       // change label text to tell the user update succeeded
       label.Text = "Message";
       string js = "function hideLabel(){document.getElementById('" + label.ClientID + "').style.display = 'none'};setTimeout(hideLabel, 5000);"
       ClientScript.RegisterClientScriptBlock(this.GetType(), "test", js ,true);
    }
    catch(Exception ex){...}
}

Are you posting back via Ajax or a normal postback? 您是通过Ajax回发还是通过普通回发? If it's a normal postback, just register some javascript on the page that sets a timer and calls your function that hides the label after the timer expires. 如果是正常的回发,只需在页面上注册一些JavaScript即可设置计时器,并调用计时器到期后隐藏标签的函数。 The following example uses jQuery to make sure the DOM is loaded before the timer is started, but there are other ways. 下面的示例使用jQuery确保在启动计时器之前已加载DOM,但是还有其他方法。 It will hide the label after 5s has elapsed. 经过5秒后,它将隐藏标签。

function hideLabel(label)
{
   $(label).hide();
}

$(document).ready( function() {
    var label = $('#labelID');
    setTimer(function() { hideLabel(label); ),5000);
});

The idea is basically the same if you are using AJAX, except you set the timer in the onSuccess callback for the AJAX call instead of on document load. 如果您使用的是AJAX,则基本思路是相同的,只是您在onSuccess回调中为AJAX调用而不是在文档加载时设置了计时器。

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

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