最近的項(xiàng)目中一直在使用atlas july ctp,自從atlas正式更名后,連續(xù)推出了beta和beta2兩個版本,但是我一直在觀望,原因就是beta版本不穩(wěn)定而且升級太麻煩了。一日偶然看到了楊丹的這篇隨筆,突然發(fā)現(xiàn)原來scriptmanager可以注冊在客戶端部分刷新的updatepanel中注冊啟動后執(zhí)行的javascript!這可是困擾了我很長時間的一個問題!這樣不但可以在使用updatepanel的情況下使用javascript彈出對話框,也可以在開發(fā)使用javascript腳本的服務(wù)器控件的時候,使控件的起動腳本得以執(zhí)行,這樣原來的和updatepanel不兼容的控件可以很容易的改為ajax enabled^_^。
不過,令人不解的是不知道處于什么考慮,registerstartupscript函數(shù)竟然實(shí)現(xiàn)為了類的靜態(tài)函數(shù),可是沒有scriptmanager的updatepanel更本不能執(zhí)行啊,這樣還需要多打幾個字!另外,需要注意的是第一個參數(shù)是updatepanel的實(shí)例,如果在頁面中有多個updatepanel,如果每個updatepanel的updatemode都是“always”[默認(rèn)值]的話,那么你使用那個updatepanel的實(shí)例作為參數(shù)都可以;如果每個updaepanel的updatemode=conditional,那么你必須使用正在更新的那個updatepanel作為參數(shù),這樣腳本才能起作用。下面是我作的一個簡單的示例:
頁面標(biāo)記
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:scriptmanager id="scriptmanager1" enablepartialrendering="true" runat="server">
</asp:scriptmanager>
<br />
<asp:updatepanel id="updatepanel1" updatemode="conditional" runat="server"><contenttemplate>
<asp:textbox id="textbox1" runat="server" ></asp:textbox> <asp:linkbutton id="linkbutton1" runat="server" onclick="linkbutton1_click">linkbutton</asp:linkbutton>
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="linkbutton1" />
</triggers>
</asp:updatepanel>
<asp:updatepanel id="updatepanel2" rendermode="block" updatemode="always" runat="server">
<contenttemplate>
<asp:textbox id="textbox2" runat="server"></asp:textbox>
<asp:linkbutton id="linkbutton2" runat="server" onclick="linkbutton2_click">linkbutton</asp:linkbutton>
</contenttemplate>
</asp:updatepanel>
<br />
<div>
<br />
<asp:updateprogress id="updateprogress1" runat="server"><progresstemplate>
please waiting, retrieving data from server
</progresstemplate>
</asp:updateprogress>
</div>
</form>
</body>
</html>
代碼
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void linkbutton1_click(object sender, eventargs e)
{
system.threading.thread.sleep(1000);
textbox1.text = datetime.now.tolongtimestring();
textbox2.text = textbox1.text;
string js = "alert('ok button1 clicked!')";
microsoft.web.ui.scriptmanager.registerstartupscript(updatepanel2, this.gettype(), "btn1clicked", js, true);
}
protected void linkbutton2_click(object sender, eventargs e)
{
system.threading.thread.sleep(900);
textbox2.text = datetime.now.tolongtimestring();
textbox1.text = textbox2.text;
string js = "alert('ok button 2 clicked!')";
microsoft.web.ui.scriptmanager.registerstartupscript(updatepanel2, this.gettype(), "btn2clicked", js, true);
}
} btw,我在使用時發(fā)現(xiàn)一個問題,不知道是個bug還是我使用不當(dāng),我在調(diào)試中發(fā)現(xiàn)updatepanel.isinpartialrendering的值始終是false!希望有高人指點(diǎn)一下:)。
新聞熱點(diǎn)
疑難解答
圖片精選