--------------------------------代碼實現非同步回傳--------------------------------------------
頁面關鍵代碼
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now %>
<asp:Button ID="Button2" runat="server" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
後台代碼
public partial class Demo2_API : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//讓button1能夠引發非同步回傳(且button1不在UpdatePanel中)
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.Button1);
//讓button2能夠引發同步回傳(且button2在UpdatePanel中)
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.Button2);
}
protected void Button1_Click(object sender, EventArgs e)
{
this.UpdatePanel1.Update();//更新UpdatePanel1中的內容
}
}
----------------------非同步回傳時彈出javascript指令碼-----------------------
頁面關鍵代碼
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<%= DateTime.Now %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<%= DateTime.Now %>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
後台代碼:
public partial class Demo5_ScriptManagerReg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
//在UpdatePanel1更新時彈出javascript指令碼
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
//在UpdatePanel2更新時彈出javascript指令碼
ScriptManager.RegisterStartupScript(this.UpdatePanel2, this.GetType(), "UpdatePanel2", "alert(2)", true);
}
}
-----------------------利用代碼(建立UpdatePanel)實現AJAX效果-----------------------------
頁面代碼:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
後台代碼:
public partial class Demo7_ProgramicUpdatePanel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel1";
this.Form.Controls.Add(up);
LiteralControl lc = new LiteralControl(DateTime.Now.ToString());
up.ContentTemplateContainer.Controls.Add(lc);
Button btn = new Button();
btn.Text = "Refresh";
up.ContentTemplateContainer.Controls.Add(btn);
}
}
--------------------AJAX非同步更新的錯誤處理-----------------------------------------------------
可以通過ScriptManager控制項的OnAsyncPostBackError事件和AsyncPostBackErrorMessage屬性捕捉和設定回傳時的錯誤訊息
樣本:
頁面代碼:
<body><br /> <form id="form1" runat="server"><br /> <div><br /> <asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError"><br /> </asp:ScriptManager></p><p> </div><br /> <asp:UpdatePanel ID="UpdatePanel1" runat="server" ><br /> <ContentTemplate><br /> <asp:TextBox ID="TextBox1" runat="server" Width="56px"></asp:TextBox><br /> <asp:Label ID="Label1" runat="server" ForeColor="DarkViolet" Text="+" Width="13px"></asp:Label><br /> <asp:TextBox ID="TextBox2" runat="server" ForeColor="DarkOrchid" Width="67px"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="計算" /><br /> <asp:Label ID="Label2" runat="server" ForeColor="#0000C0" Text="Label"></asp:Label><br /> </ContentTemplate><br /> </asp:UpdatePanel><br /> </form><br /></body>
後台代碼:
public partial class UpdatePanelErrorMessage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(TextBox1.Text.Trim());
double b = Convert.ToDouble(TextBox2.Text.Trim());
double res = a * b;
Label2.Text = res.ToString("f3");//輸出字串格式的控制
}
catch (Exception ex)
{
if (TextBox1.Text.Length > 1 && TextBox2.Text.Length > 1)
{
ex.Data["cuo"] = "這兩個數無法相乘!";
}
throw ex;
}
}
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
if (e.Exception .Data ["cuo"]!=null )
{
ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Data["cuo"].ToString();
}
else
{
ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message;
}
}
}