2011-12-12 134 views
1

我使用<的div的onclick =“”“>调用服务器端我。如何才能避免整页刷新,当用户点击< DIV>?div onclick没有整页重新加载?

,这里是我的编码。

HTML

<asp:UpdatePanel ID="updatePanel1" runat="server"> <ContentTemplate> 
<div id ="cde" runat="server" style="background-color: #00FFFF" > </div> 
    <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label> 
</ContentTemplate> </asp:UpdatePanel> 

服务器端

protected void Page_Load(object sender, EventArgs e) 
    { 
     //--Register for div onclick function -- 
     cde.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this, "divMember_Click"); 
     //**Register for div onclick function** 
    } 
protected void divMember_Click() 
    { 

     this.Label1.Text = System.DateTime.Now.ToString(); 

    } 
    public void RaisePostBackEvent(string eventArgument) 
    { 
     if (!string.IsNullOrEmpty(eventArgument)) 
     { 

      if (eventArgument == "divMember_Click") 
      { 

       divMember_Click(); 

      } 

     } 
    } 

我尝试把标签更新面板内,但POS tback仍然会发生。我如何刷新标签没有整页重新加载?

+0

您是否有ScriptManager元素? – Russell

回答

0

你可以使用jQuery阿贾克斯函数或pagemethod.YourWebMethod。

在C#代码中创建Web方法,并使用这两个函数调用它。

您可以在脚本管理器中通过EnabledPageMethod =“true”来完成页面方法,并使用jquery .ajax函数放入jquery java脚本。

这将在后面的代码页(创建Web方法):

public partial class _Default : Page 
{ 

    [WebMethod] 

    public static string GetDate() 



{ 

回报DateTime.Now.ToString();

} 

} 

如Default.aspx中引用,这是Java脚本:

$(document).ready(function() { 
    // Add the page method call as an onclick handler for the div. 
    $("#Result").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "Default.aspx/GetDate", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
     // Replace the div's content with the page method's return. 
     $("#Result").text(msg.d); 
     } 
    }); 
    }); 
}); 

    <head> 
    <title>Calling a page method with jQuery</title> 
    <script type="text/javascript" src="jquery-1.2.6.min.js"></script> 
    <script type="text/javascript" src="Default.js"></script> 
</head> 
<body> 
    <div id="Result">Click here for the time.</div> 
</body> 
0

把DIV和标签为的UpdatePanel

+0

嗨,我尝试adi.i已经把标签和div里面的updatePanel,但回发将仍然会发生:( – user998405

+0

Works for me http://pastebin.com/YCjPjKWm –

0

尝试这种方式,

请更新面板ID “Updatepanel1” 您的标签具有以下属性

Update Mode : Conditional 
EnableViewState : true(if you are using it) 
+0

u意味着udatePanel控件属性?我尝试设置更新模式为条件但仍然相同:( – user998405

+0

为了部分加载页面,使用多个更新面板中的页面 – AnandMohanAwasthi

+0

我尝试在页面中使用多个更新panal,但它仍然会回发,mayb你给我一个示例? – user998405

0

你需要有一个asp:ScriptManager的当前页面上使用的UpdatePanel。请参阅here

相关问题