1.在服务器端代码中嵌入Javascript(1)RegisterStartupScript 和 RegisterClientScriptBlock。已经淘汰。
(2) Page.ClientScript.RegisterStartupScript() 方法
语法1:RegisterStartupScript (type, key, script)
语法2:RegisterStartupScript (type, key, script, script tag specification)
(3)Page.ClientScript.RegisterClientScriptBlock() 方法
RegisterStartupScript 方法是将 JavaScript 嵌入到 ASP.NET 页面的底部,恰好位于关闭元素 </form> 的前面。RegisterClientScriptBlock 方法是将 JavaScript 嵌入到页面中开启元素 <form> 的紧后面。
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),MyScript","MyJavaScriptFile.js");
2.在Javascript中运行服务器端代码(1)触发服务器控件事件
如触发服务器控件button的OnClick事件。
document.getElementById("btn1").click();
(2)使用<%=方法或属性%>
Var data = '<%=GetDemandDataString("' + PageIndex.toString() + '") %>';
可以无参数,如果有参数必须是字符类型,缺点是只在编译时取值,取值后不变
其中GetDemandDataString为服务端方法。
(3)使用__doPostBack方法
<input id="Button1" type="button" name="Button1" value="按钮" onclick="javascript:__doPostBack(’Button1’,’’)">
__doPostBack的源代码如下:
<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>
如果不想用控件来扑捉,可以在Page_Load事件中获取
用Request.Form["__EVENTTARGET"]和Request.Form["__EVENTARGUMENT"]取得控件ID和参数.
(4) 利用ScriptManager控件的PageMethods对象
注意一定要设置EnablePageMethods="true",而且所调用的后台方法必须是WebMethod静态方法,示例:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
function myLazyLoad(grid, e) {
//第一页在页面Page_Load中加载
var PageIndex;
if (e.currentPageIndex <= 1) {
PageIndex = 1;
}
else {
PageIndex = e.currentPageIndex;
}
PageIndex = PageIndex + 1;
PageMethods.GetDemandDataString(PageIndex, e.pageSize,myLazyLoad_OnSucceeded, myLazyLoad_OnFailed);
e.currentPageIndex = PageIndex;
}
//成功时的处理方法
function myLazyLoad_OnSucceeded(result) {
var dom = new ActiveXObject("MSXML.DOMDocument");
dom.loadXML(result);
var grid = document.getElementById("<%=SmartGrid2.ClientID %>");
grid.append(dom.documentElement);
}
//失败时的处理方法
function myLazyLoad_OnFailed(error) {
}
后台代码示例:
//懒加载某页数据
[System.Web.Services.WebMethod]
public static string GetDemandDataString(int pageIndex,int pageSize)
{
XmlDocument doc;
string selectCmdString = "Select TOP(" + pageSize.ToString() + ") * FROM (Select *,"
+ " ROW_NUMBER() OVER (ORDER BY GID) AS ROWNUM FROM Storage_Goods as tab) as tab"
+ " Where tab.ROWNUM > " + ((pageIndex - 1) * pageSize).ToString()
+ " ORDER BY tab.ROWNUM";
DataSet currentDs = new DataSet();
using (SqlConnection currentConn = new SqlConnection(_connectionstring))
{
//获取数据
currentConn.Open();
SqlDataAdapter currentSda = new SqlDataAdapter(selectCmdString, currentConn);
currentSda.Fill(currentDs, "Storage_Goods");
doc = MyConvertDataTableToXML(currentDs.Tables["Storage_Goods"]);
currentConn.Close();
}
return doc.InnerXml;
}
(5)使用AJAX
示例1:
//客户端代码
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Msxml2.XMLHTTP');
function ClearCache() {
xmlhttp.open("GET", "ClearCacheHelper.aspx?CacheKey=SmartGrid1.DataSource&&Random=" + Math.random().toString(), false);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
…
}
}
xmlhttp.send();
}
//服务器端代码
protected void Page_Load(object sender, EventArgs e)
{
//处理传递过来的参数
if (Request.Params.Count > 0)
{
string cacheKey = Request.Params["CacheKey"];
if (cacheKey != null)
{
HttpRuntime.Cache.Remove(cacheKey);
System.Web.UI.ScriptManager.RegisterStartupScript(Page, Page.GetType(), new Random().Next().ToString(), "setTimeout('window.close();',0);", true);
return;
}
}
}
示例2
//客户端代码
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Msxml2.XMLHTTP');
function fn_LoadData() {
xmlhttp.open("GET", window.location+"?LoadData=1", false);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var dom = new ActiveXObject("MSXML.DOMDocument");
dom.loadXML(xmlhttp.responseText);
var grid = document.getElementById("<%=SmartGrid1.ClientID %>");
grid.clear();
alert(xmlhttp.responseText);
grid.focus();
grid.append(dom.documentElement);
}
}
xmlhttp.send();
}
//服务器端代码
protected void Page_Load(object sender, EventArgs e)
{
//处理传递过来的参数
if (Request.Params.Count > 0)
{
string loadData = Request.Params["LoadData"];
if (loadData != null && loadData =="1")
{
loadData = GetLoadDataString();
Response.Write(loadData);
Response.End();
return;
}
}
}
r