Using jQuery BlockUI Plugin with ASP.NET Input Form

15. July 2009

I was recently working on an ASP.NET WebForm project where it made sense to use a modal dialog to confirm choices, to ask for additional information, etc…

I’ve used the jQuery BlockUI Plugin before and I’ve always thought it provided a good mix of nice features as well as extreme customization.

Ii went ahead an implemented BlockUI across the application and was satisfied on how it looked & felt, but I noticed some weird behavior on one of the modal screens. I had a screen that had numerous input controls (textboxes, dropdownlists, etc…) and when I posted back my results all of the input controls were blank!

Ahh, what do I do? Where are my updated input values?

Debugging Steps

  1. Check to make sure I wasn’t re-initializing the input controls on PostBack
  2. Investigate that I didn’t turn off viewstate by accident & make sure I wasn’t using dynamic controls

After those steps I was still confused and frankly wasting a lot of time with something I thought would be very quick.

To make a long story short, it was BlockUI that was clearing the input controls on PostBack!

So, I created a fresh demo application to replicate the issue and started investigating.

The following code shows how to recreate the issue and how to resolve the issue.

    <div>
        <asp:Panel ID="pnlMessage" runat="server" Visible="false">
            <asp:Label ID="lblMessage" runat="server" /><br />
        </asp:Panel>
        <asp:Button id="ctlAddContact" runat="server" Text="Add Contact" />
    </div>
    
    <div id="ctlAddContactModal" style="display: none;" class="modal">
        <h3>Add Contact</h3>
        <dl>
            <dt><asp:Label ID="lblFirstNameCaption" runat="server" AssociatedControlID="txtFirstName" Text="First Name" /></dt>
            <dd><asp:TextBox ID="txtFirstName" runat="server" /></dd>
            <dt><asp:Label ID="lblLastNameCaption" runat="server" AssociatedControlID="txtLastName" Text="Last Name" /></dt>
            <dd><asp:TextBox ID="txtLastName" runat="server" /></dd>
        </dl>
        <div class="buttons">
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
            <asp:LinkButton ID="btnFailureLink" runat="server" Text="Failure Save" CssClass="action" OnClick="btnFailureLink_Click" />
            <asp:LinkButton ID="btnSuccessLink" runat="server" Text="Success Save" CssClass="action" onclick="btnSuccessLink_Click" />
        </div>
    </div>
     
 
    <script type="text/javascript">
        $(function() {
            $('#<%= ctlAddContact.ClientID %>').click(function(e) {
                e.preventDefault();
                $.blockUI({
                    message: $('#ctlAddContactModal'),
                    css: {
                        cursor: 'auto'
                    }    
                });
            });

            $('#<%= btnCancel.ClientID %>').click(function(e) {
                $.unblockUI();
            });

            var btnSuccessLinkId = '<%= btnSuccessLink.ClientID %>';
            $('#' + btnSuccessLinkId).click(function(e) {
                e.preventDefault();
                $.unblockUI({
                    onUnblock: function() {
                        eval($('#' + btnSuccessLinkId).attr('href'));
                    }
                });
            }); 
        });
    </script> 

The issue is more obvious if you had used an <asp:Button /> in the above code snippet instead of an <asp:LinkButton /> as was the standard in my project. When using a <asp:Button /> the modal input form doesn’t submit at all! The BlockUI prevents the modal from from posting. I tried manipulating some of the plugin settings such as setting bindEvents = false and others, but nothing seemed to help.

So, the solution that works is…

  1. Attach a jQuery click event to your LinkButton
  2. Prevent the default click behavior of the LinkButton
  3. Unblock the modal window to restore default behavior of input form
  4. Add an onUnblock event to the modal window and evaluate the LinkButton’s href

ASP.NET, jQuery ,


Comments

7/24/2009 11:56:52 PM #
Elijah, it is preferible to use a separate script when using ASP.NET and jQuery, to avoid mixing of both scripts and asp.net markup in the same page, why not try something like:

var showInputForm = {
    Init: function() {
  $(function(addContactId, cancelClientId, successLinkId) {
        $(addContact).click(function(e) {
            e.preventDefault();
            $.blockUI({
                message: $('#ctlAddContactModal'),
                css: {
                    cursor: 'auto'
                }    
            });
        });

        $(cancelClientId).click(function(e) {
            $.unblockUI();
        });

        var btnSuccessLinkId = successLinkId;
        $('#' + btnSuccessLinkId).click(function(e) {
            e.preventDefault();
            $.unblockUI({
                onUnblock: function() {
                    eval($('#' + btnSuccessLinkId).attr('href'));
                }
            });
        });
    });

    }
}

So the next time in your ASP.NET page you only need to do something like:

<script language="javascript" type="text/javascript">
     showInputForm.Init('#<%= ctlAddContact.ClientID %>', '#<%= btnCancel.ClientID %>', '<%= btnSuccessLink.ClientID %>');
</script>

Well, personally I see it more cleaner now ;)
9/19/2009 7:02:34 PM #
valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up
9/19/2009 7:23:39 PM #
Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!
9/22/2009 3:53:38 PM #
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
9/30/2009 10:00:23 PM #
Hi, a great informative post. Apart form that you have got a great useful resources over here.
10/6/2009 7:21:24 AM #
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful
10/6/2009 9:04:55 PM #
Hey - nice blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
10/8/2009 6:50:23 PM #
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful well l
10/15/2009 9:05:38 PM #
The Tampa personal injury lawyers of Beltz & Ruth have multiple offices in the Tampa/St. Petersburg area to provide the best possible legal representation for you and your family.
10/15/2009 9:14:56 PM #
Orlando personal injury lawyer, Todd Miner is a former insurance company attorney who works for the rights of personal injury victims. He handles a vareity of cases like personal injury, car accidents, premises liability, workers' compensation, and wrongful death.

10/26/2009 1:53:38 PM #
Hi, thanks for sharing a nice information. I like Blog Engine platform of blog as it is based on .net technology, it makes an ease to work with it. Apart from that a great community help is made available under the same platform.
12/1/2009 11:35:35 AM #
Hi,

Why does my Keyboard on my laptop not work or sometimes repeat \ or nos? What is wrong? Help...?
12/1/2009 11:36:30 AM #
Hi,

I have a dell laptop and sometimes for no apparent reason,the keyboard & mousepad completely stop working.?
12/17/2009 8:25:49 PM #
I had used ASP.NET. But now, I am not using as I have moved to another sector. However, your post on "Using jQuery BlockUI Plugin with ASP.NET Input Form" gives me more ideas about ASP. Cheers for share.
khairul
khairul
12/23/2009 6:28:23 PM #
i got some problem.
thx elijah..you save my time.
trial1
trial1
12/25/2009 12:32:08 PM #
Hi Elijah,

What would be the changes if you tries to abstract the whole Add contact function including its buttons a separate user control called AddContact.ascx. What would be the changes needed to the scripts as the page will not have access to the cancel, success button directly anymore?

thx
12/30/2009 10:36:02 AM #
Hi,

I bought 2 computers on second sale. Please inform how do I use these computers to earn regular income.?

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Olark Livehelp