2011-08-11 52 views
1

我在與一個文本框和一個按鈕asp.net ajax的問題​​

文本框有它的onblur事件這是使用頁面加載後jQuery的設置形式。

該按鈕具有設置文本框中的某些文本的單擊事件。問題是,無論什麼時候發生,onblur事件都會消失。如何解決這個問題。

這只是一個例子。我在頁面上有幾個控件使用jquery設置了一些值。所有的值在他的下拉ajax事件後都會丟失。

這是一個示例代碼,顯示問題

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 

    <script language="javascript"> 

     var ctrl_name = "#<%=name.ClientID %>"; 

     $(function() { 
      $(ctrl_name).blur(
       function() { 
        alert("this texbox has lost its focus"); 
       } 

      ); 
     }); 

    </script> 


</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 



     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:TextBox ID="name" runat="server"></asp:TextBox> 
      <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
     </ContentTemplate> 
     </asp:UpdatePanel> 

    </div> 
    </form> 
</body> 
</html> 

代碼隱藏代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      name.Text = "testing Ajax"; 
     } 
    } 
} 
+1

你需要發佈一些代碼,任何人能夠回答這 –

+0

將添加代碼 –

+0

添加示例代碼 –

回答

0

你在頁面上失去你的價值觀的原因是你的button_click是一個服務器端方法,這將始終導致頁面重新加載。

您需要使方法可以從客戶端調用,以便在ajax中使用它。它需要成爲WebMethod或ScriptMethod。

我最近沒有使用這個東西,所以我的語法可能不是100%正確的,但希望這可以指出你在正確的方向。

你的按鈕點擊方法需要在窗體:

[WebMethod] 
public static void Button1_Click() 
{ 
... 
} 

,你這樣稱呼它:

<input ID="Button1" Text="Button" onclick="PageMethods.Button1_Click()" />