JQuery 更改函数在使用母版页和更新面板时会导致整页回发


$('#bn1').click(function () {
    $('#textbox1').change();
})
<asp:UpdatePanel runat="server" ID="up123" UpdateMode="Conditional">
   <ContentTemplate>
      <div style="margin-top: 250px;">
         <asp:TextBox runat="server" ID="textbox1" ClientIDMode="Static" AutoPostBack="true" CssClass="DateTimePicker" />
         <asp:Button runat="server" ID="bn1" ClientIDMode="Static" Text="clickme" />
      </div>
   </ContentTemplate>
</asp:UpdatePanel>

在我的内容页面中将上述函数与母版页一起使用时,它会导致整页回发而不是部分回发如果我在没有母版页的情况下使用它,那完全没问题,有什么想法吗?

Put return false 以阻止服务器端单击事件的发生,如下所示:

$('#bn1').click(function () {
    $('#textbox1').change();
    // This stops the server control from doing a click, which 
    // posts the form back to the server
    return false;
});

更新:

若要验证哪个控件实际导致回发到服务器,请在Page_Load事件中执行此操作:

protected void Page_Load(object sender, EventArgs e)
{
    var controlIdThatCausedPostBack = String.Empty;
    var scriptManager = ScriptManager.GetCurrent(Page);
    if (scriptManager != null)
    {
        var smUniqueId = scriptManager.UniqueID;
        var smFieldValue = Request.Form[smUniqueId];
        if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|"))
        {
            controlIdThatCausedPostBack= smFieldValue.Split('|')[1];
        }
    }
    else
    {
        controlIdThatCausedPostBack = Page.Request.Params["__EVENTTARGET"];
    }
    if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack))
    {
        // Here we have the control ID that causes the post back
    }
}

最新更新