ASP.NET with jQueryUI: textbox value becomes null on Button click event
I have an ASP.NET page where I have a button. When the user clicks the button, I will check if the user is logged in or not. If you are not logged in, I will show a modal login popup (using jQueryUI). I have put one textbox (txtPassword) and one control (btnLogin) in a Div that will be shown by jQueryDialog. But in the btnLogin Click event, I cannot read the text value entered in the txtPassword textbox
Below is my code
<form id="form1" runat="server">
<div>
<br />
<asp:TextBox ID="txtModelId" runat="server" Text=""></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 TradeIn Account">
<p id="validateTips">Enter your EmailId & password</p>
<fieldset>
<label for="email">Email</label>
<asp:TextBox ID="txtEmail" CssClass="text ui-widget-content ui-corner-all" runat="server" ></asp:TextBox>
<label for="password">
<asp:TextBox ID="TextBox1" runat="server" Text="sample"></asp:TextBox>Password</label>
<asp:TextBox ID="txtPassword" CssClass="text ui-widget-content ui-corner-all" runat="server" ></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" UseSubmitBehavior="false"/>
</fieldset>
</div><!--dialog-->
</div><!--demo-->
</div><!--divContentHolder-->
</form>
Server side code
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");
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string txtSample= TextBox1.Text; // this is showing the value 'sample'
string txtPass= txtPassword.Text; // this is showing null
if (txtPass == "shyju")
{
Session["username"] = txtPassword.Text;
Response.Redirect("TestingModal.aspx");
}
}
My java script code to render dialog
$ (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");
}
});
I had a hard-coded value of the text properties of TextBox1 as a "sample" in the HTML part of my ASPX file. In the button click event, I get it. But another textbox, txtPassword Text property gives me a null value
I ask you to advise.
Thanks in advance
+1
a source to share