JavaScript 确认对话框 - ASP.NET

发布于 2024-07-13 12:01:24 字数 684 浏览 4 评论 0原文

我看到了关于同一主题的几个类似问题,但没有一个解决我的问题。

我有一个 asp.net 网站,并且想在数据库更新后向用户显示状态消息(asp:label 将在 5 秒内消失)。

我想将文本分配给标签,然后用 Javascript 隐藏它。

我已经解决了 js 部分,唯一的问题是在将文本分配给控件后如何调用 js 函数?

假设我正在使用以下代码更新数据库中的某些内容:

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

代码隐藏

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

请帮忙!

更新:请不要使用 jquery,只需简单的 javascript

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.

I want to assign the text to the label, and then hide it with 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-07-20 12:01:24

我个人会使用 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>

如果需要,您还可以将脚本块嵌入到 Literal 控件中,并且仅在更新标签文本时使其可见。

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:

<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.

ゝ杯具 2024-07-20 12:01:24

回答您的问题“将文本分配给控件后如何调用 js 函数?”。 您只需在按钮单击事件中添加对“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){...}
}

To answer your question "How do i call js function after i assigned the text to the control?". You can just add a call to 'RegisterClientScriptBlock' inside of your button click event to output the JavaScript provided by Luke.

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){...}
}
国际总奸 2024-07-20 12:01:24

您是通过 Ajax 回发还是普通回发? 如果是普通的回发,只需在设置计时器的页面上注册一些javascript,并在计时器到期后调用隐藏标签的函数即可。 下面的示例使用 jQuery 确保在计时器启动之前加载 DOM,但还有其他方法。 5 秒后它将隐藏标签。

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

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

如果您使用 AJAX,这个想法基本上是相同的,只不过您在 AJAX 调用的 onSuccess 回调中设置计时器,而不是在文档加载时设置计时器。

Are you posting back via Ajax or a normal postback? 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. The following example uses jQuery to make sure the DOM is loaded before the timer is started, but there are other ways. It will hide the label after 5s has elapsed.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文