div onclick没有完整页面重新加载?

时间:2011-12-12 04:29:36

标签: asp.net ajax onclick updatepanel

我正在使用< div onclick =“”“>调用服务器端。当用户点击< div>时,如何避免整页刷新?

这是我的编码。

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();

            }

        }
    }

我尝试将标签放在更新面板中。但是回发仍然会发生。如何在没有完整页面重新加载的情况下刷新标签?

4 个答案:

答案 0 :(得分:0)

将div和Label放入UpdatePanel

答案 1 :(得分:0)

试试这种方式,

将您的标签保留在更新面板ID“Updatepanel1”中,并带有以下属性

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

答案 2 :(得分:0)

你可以使用Jquery .ajax函数或pagemethod.YourWebMethod。

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

您可以在脚本管理器中通过EnabledPageMethod =“true”执行Page Method,并使用Jquery .ajax函数放置jquery java脚本。

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

public partial class _Default : Page 
{

  [WebMethod]

  public static string GetDate()



{

return 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>

答案 3 :(得分:0)

您需要在页面上显示一个asp:ScriptManager才能使用UpdatePanel。请参阅here

相关问题