[英]Javascript confirmation dialog - 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
我個人使用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
控件中,並僅在更新標簽文本時使其可見。
回答您的問題“將文本分配給控件后,如何調用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){...}
}
您是通過Ajax回發還是通過普通回發? 如果是正常的回發,只需在頁面上注冊一些JavaScript即可設置計時器,並調用計時器到期后隱藏標簽的函數。 下面的示例使用jQuery確保在啟動計時器之前已加載DOM,但是還有其他方法。 經過5秒后,它將隱藏標簽。
function hideLabel(label)
{
$(label).hide();
}
$(document).ready( function() {
var label = $('#labelID');
setTimer(function() { hideLabel(label); ),5000);
});
如果您使用的是AJAX,則基本思路是相同的,只是您在onSuccess回調中為AJAX調用而不是在文檔加載時設置了計時器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.