开发者

jQuery Mobile and ASP.NET Update Panel problem

I have an ASP.NET website that also utilizes jQuery Mobile for rendering. Mostly, this works very well, however I am having one small problem. The site runs a survey. There are several "submit" buttons on the page. In order to keep the entire site from reloading, the Submit buttons are in UpdatePanels, so that only the button re-renders.

(I should also mention that we have a Report control on another page, that is having the same problem I'm about to describe).

When the submit button is clicked, the button re-renders, however now it looks like a "normal" button, as all the CSS added by jQuery Mobile is now lost.

So... I know that the problem is that, because the entire page is not reloading, jQuery Mobile is not intercepting the postback and injecting the necessary CSS to the button.

What I don't k开发者_如何学JAVAnow is how to fix it.

I think I know enough to make a javascript call on the postback, but I don't know what jQuery Mobile routine to call (I assume there is some kind of document.Ready() call, but heck if I can find it) in order to fix it.

I did post this question on the jQuery Mobile site and did not get a reply. I'm kind of desperate at this point as the client is breathing down my neck about it.

Any help would be appreciated.


I found a solution to this and wanted to post it here for others. My solution is slightly overkill, but as such should "just work" in most cases. At the end of the day, what you need to do is trigger the "create" event on the element(s) that need updating. This is what jQuery Mobile is looking for and what kicks it off to re-parse the HTML. The following statement will grab all <div>'s (which naturally includes all update panels) on the page, and will re-render any controls within them. I added it to the submit button's Click() event in the ASP.NET code behind. (Note that adding the javascript to the button's client side onclick() didn't seem to work)

ScriptManager.RegisterStartupScript(Page, GetType(), "temp", "<script type='text/javascript'>$('div').trigger('create');</script>", false);  

Be aware that if you have a better handle on the content you want to re-parse, then instead of grabbing every single <div> on the page, you could target the exact <div> that needs updating and just update only what is necessary. This would provide better performance, however the parsing is pretty darn fast even on a complicated page.


UpdatePanel essentially means that you have ASP.NET AJAX running in your site.
pageLoad ( not exactly DOM Ready ) is called after every partial postback. Work with that event and write your corrective code in that.

function pageLoad(sender, args){
    //Jquery Code
}

P.S: UpdatePanels are really dangerous. I have had my fair share of tears with that.
Please do not use that together with another libraries in future.

Hope this helps, really.


You need to Manage partial-page updates of server UpdatePanel controls in the browser. Read More: Sys.WebForms.PageRequestManager . Please check my two pages' (master & child) codes.

Master Page

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="App.Site" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>NFP5</title>
    //All CSS references

    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
    </head>
<body>
<form id="frmPage" runat="server">
    <asp:ScriptManager ID="sm1" runat="server">

    </asp:ScriptManager>

    <asp:ContentPlaceHolder ID="MainContent" runat="server">

    </asp:ContentPlaceHolder>
</form>
<script><!--All JSreferences--></script>
</body>
</html>

Child Page

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeBehind="WebForm1.aspx.cs" Inherits="App.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
    <script type="text/javascript">
        function pagLoad() {
            $('.txtID').Zebra_DatePicker({
                format: 'd/m/Y',
                default_position: 'below'
            });
        }
        $(document).ready(pagLoad);
    </script>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pagLoad);
    </script>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtID" runat="server" CssClass="validate[required] txtID"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" OnClientClick="javascript:$('#frmPage').validationEngine('attach'); return $('#frmPage').validationEngine('validate');" Text="Button" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜