在将记录插入数据库后,我正在调用jquery函数…
ScriptManager.RegisterClientScriptBlock(LbOk, typeof(LinkButton), "json",
"topBar('Successfully Inserted');", true);
我已将此内容包含在母版页中,用于在回发后执行jquery函数,
<script type="text/javascript">
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
function EndRequestHandler()
{
topBar(message);
}
function topBar(message) {
alert(a);
var alert = $('<div id="alert">' + message + '</div>');
$(document.body).append(alert);
var $alert = $('#alert');
if ($alert.length) {
var alerttimer = window.setTimeout(function() {
$alert.trigger('click');
}, 5000);
$alert.animate({ height: $alert.css('line-height') || '50px' }, 200).click(function() {
window.clearTimeout(alerttimer);
$alert.animate({ height: '0' }, 200);
});
}
}
</script>
<body onload="load();">
但似乎没用…任何建议..
最佳答案
下面是一个完整的工作示例:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" %>
<script type="text/C#" runat="server">
protected void BtnUpdate_Click(object sender, EventArgs e)
{
// when the button is clicked invoke the topBar function
// Notice the HtmlEncode to make sure you are properly escaping strings
ScriptManager.RegisterStartupScript(
this,
GetType(),
"key",
string.Format(
"topBar({0});",
Server.HtmlEncode("Successfully Inserted")
),
true
);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
function topBar(message) {
var alert = $('<div id="alert">' + message + '</div>');
$(document.body).append(alert);
var $alert = $('#alert');
if ($alert.length) {
var alerttimer = window.setTimeout(function () {
$alert.trigger('click');
}, 5000);
$alert.animate({ height: $alert.css('line-height') || '50px' }, 200).click(function () {
window.clearTimeout(alerttimer);
$alert.animate({ height: '0' }, 200);
});
}
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<asp:ScriptManager ID="scm" runat="server" />
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<!--
You could have some other server side controls
that get updated here
-->
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="BtnUpdate"
EventName="Click"
/>
</Triggers>
</asp:UpdatePanel>
<asp:LinkButton
ID="BtnUpdate"
runat="server"
Text="Update"
OnClick="BtnUpdate_Click"
/>
</form>
</body>
</html>