【问题标题】:JQuery change function cause full page postback when using masterpage and updatepanel?使用 masterpage 和 updatepanel 时,JQuery 更改功能会导致整页回发?
【发布时间】:2013-12-11 18:14:45
【问题描述】:
$('#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>

在我的内容页面中将上述功能与 masterpage 一起使用时,会导致整个页面回发而不是部分回发 如果我在没有母版页的情况下使用它,那很好,有什么想法吗?

【问题讨论】:

    标签: jquery asp.net updatepanel master-pages


    【解决方案1】:

    输入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
    
        }
    }
    

    【讨论】:

    • 它不起作用,它仍然导致整页回发,谢谢你的回复。
    • 再次,如果我不使用母版页,一切正常,按钮触发部分回发
    • @JoeLu - 你确定是按钮吗,因为你的TextBox 服务器控件有AutoPostBack="True",这意味着当它失去焦点时它会回发到服务器?
    • @JoeLu - 我已经更新了我的答案,将Page_Load 中的逻辑包含在内,该逻辑将告诉您实际上是哪个控件将帖子返回到服务器。
    • 不好意思问题不清楚,其实是jquery函数change()引起的
    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2010-09-18
    • 1970-01-01
    • 2010-10-18
    相关资源
    最近更新 更多