Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET with jQueryUI: server side event not firing

I have an ASP.NET page where the page is making use of jQuery UI dialog. When a user is clicking a button (btnGo) in a page, I will check if the user is logged in or not. If not logged in, I would show the jQuery UI dialog to login. I used this code:

<body>
<form id="form1" runat="server">
<div>
   <br />  
    <asp:TextBox ID="txtModelId" runat="server" Text="12"></asp:TextBox><br />

   <asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
    <br />
    <asp:Label ID="lblUserMsg" runat="server" Text="Label"></asp:Label></div>
    <div id="divContentHolder">
    <div class="demo">

    <div id="dialog" title="Login with your User Account">
    <p id="validateTips">Enter your EmailId & password</p>


    <fieldset>

    <label for="email">Email</label>
    <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
    <label for="password">Password</label>
    <asp:TextBox ID="txtFirstName" CssClass="text ui-widget-content ui-corner-all" runat="server" Text=""></asp:TextBox>
  <!-- &nbsp;<asp:Button   ID="btnSingIn" runat="server" OnClick="btnSingIn_Click" Text="Button" /><br />-->
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
    </fieldset>

    </div><!--dialog-->
    </div><!--demo-->
   </div><!--divContentHolder-->

</form>

And on the server side:

protected void btnGo_Click(object sender, EventArgs e)
    {
        CheckUserLoggedIn();
    }

private void CheckUserLoggedIn()
    {
        if (Session["username"] != null)
        {
            Response.Write("Logged in user");
            ClientScript.RegisterHiddenField("isAuthenticated", "true");
        }
        else
        {
            ClientScript.RegisterHiddenField("isAuthenticated", "false");
        }
    }

The above code works fine somewhat. If a user is not logged in, it will show the jQuery UI Dialog to login. But the problem is, The server side function btnLogin_Click() (Click event of ASP.NET button btnLogin) is not getting fired. I tried it in another test page by placing a form tag just before the fieldset and it worked. Ex:

<form id="form1" runat="server">
 <fieldset>  then  rest of the items...(text box and button)

But I can't do this in the first page, because if I place the form tag just before the field set, my Other ASP.NET text box and button would be out of the form and when trying to run it is showing an error, telling that the txt box control has to be in the form.

I know we can't use multiple forms in this page with runat="server".

Whats the solution for this problem?

The javascript code is:

<script type="text/javascript">
$(function() {

    var name = $("#name"),
        email = $("#email"),
        password = $("#password"),
        allFields = $([]).add(name).add(email).add(password),
        tips = $("#validateTips");

    function updateTips(t) {
        tips.text(t).effect("highlight",{},1500);
    }

    function checkLength(o,n,min,max) {

        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between "+min+" and "+max+".");
            return false;
        } else {
            return true;
        }

    }

    function checkRegexp(o,regexp,n) {

        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }

    }

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 300,
        modal: true,
        buttons: {
            'Create an account': function() {
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid=true;
                if (bValid) {

                    /*$('#users tbody').append('<tr>' +
                        '<td>' + name.val() + '</td>' + 
                        '<td>' + email.val() + '</td>' + 
                        '<td>' + password.val() + '</td>' +
                        '</tr>'); */

                        alert("Check User Credentials")
                    $(this).dialog('close');
                }
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
    });



    $('#create-user').click(function() {
        $('#dialog').dialog('open');
    })
    .hover(
        function(){ 
            $(this).addClass("ui-state-hover"); 
        },
        function(){ 
            $(this).removeClass("ui-state-hover"); 
        }
    ).mousedown(function(){
        $(this).addClass("ui-state-active"); 
    })
    .mouseup(function(){
            $(this).removeClass("ui-state-active");
    });

//});
var isAuthenticated = $("#isAuthenticated").val();
if (isAuthenticated && isAuthenticated == "false")
{
// Display the modal dialog.
$("#dialog").dialog("open");
}
});
</script>
like image 563
Shyju Avatar asked Nov 06 '25 21:11

Shyju


1 Answers

Try setting UseSubmitBehavior=false on the button control. Apparently the jQuery BlockUI widget changes the button behavior and it doesn't submit correctly.

See: http://encosia.com/2008/10/04/using-jquery-to-enhance-aspnet-ajax-progress-indication/

like image 181
Aaron Powell Avatar answered Nov 08 '25 10:11

Aaron Powell