代碼實現AJAX非同步回傳(彈出javascript指令碼)、非同步更新的錯誤處理

來源:互聯網
上載者:User

--------------------------------代碼實現非同步回傳--------------------------------------------
頁面關鍵代碼

<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;
        }
    }
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.