I’m trying to do a login page with asp.net and jQuery mobile.
When the user click on the login button, I first do some processing in c# and then call the JavaScript.
The problem is that when I hit the login button, the page seems to reload before it executes the JavaScript, and I don’t want that.
Here is my code:
<asp:Button name="login" ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
c#:
protected void btnLogin_Click(object sender, EventArgs e)
{
if (String.Format("{0}", Request.Form["username"]) != "" && String.Format("{0}", Request.Form["password"]) != "")
{
ClientScript.RegisterStartupScript(GetType(), "key", "login();", true);
}
else
{
//todo
}
}
JS/jQuery:
<script type="text/javascript">
function login() {
jQuery(function () {
jQuery.ajax({
beforeSend: function () {
$.mobile.loading('show', {
text: 'Processing...',
textVisible: true,
theme: 'a'
});
},
type: "GET",
url: "Handler.ashx",
data: "method=validate",
success: function (data) {
$.mobile.loading('hide');
//$.mobile.changePage("HomePage.aspx", { transition: "fade" });
}
});
});
};
</script>
Any idea?
Thanks!
EDIT: I still need the server side code to deal with the password encryption that is in C#
The
OnClickevent is a server-side event. That is why it is posting back. You want to use theOnClientClickevent. This is for client-side code to run before theOnClickevent is invoked.That said, what server-side code do you need to run before calling the javascript? What exactly are you trying to do?
Please explain the process flow in more detail.