JavaScript 确认对话框 - ASP.NET
我看到了关于同一主题的几个类似问题,但没有一个解决我的问题。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人会使用 jQuery 来实现此目的,但如果你想使用普通的旧 JavaScript,那么类似这样的事情可能会做技巧:
如果需要,您还可以将脚本块嵌入到
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:
If necessary you could also embed the script block in a
Literal
control and only make it visible when you update your label text.回答您的问题“将文本分配给控件后如何调用 js 函数?”。 您只需在按钮单击事件中添加对“RegisterClientScriptBlock”的调用即可输出 Luke 提供的 JavaScript。
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.
您是通过 Ajax 回发还是普通回发? 如果是普通的回发,只需在设置计时器的页面上注册一些javascript,并在计时器到期后调用隐藏标签的函数即可。 下面的示例使用 jQuery 确保在计时器启动之前加载 DOM,但还有其他方法。 5 秒后它将隐藏标签。
如果您使用 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.
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.