带有jQueryUI的ASP.NET:“按钮单击"事件中的文本框值为null [英] ASP.NET with jQueryUI: text box value is getting as null in Button click event

查看:78
本文介绍了带有jQueryUI的ASP.NET:“按钮单击"事件中的文本框值为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET页面,其中有一个按钮当用户单击按钮时,我将检查用户是否已登录.如果未登录,我将显示一个模式弹出窗口以登录(使用jQueryUI) ).我在div中放置了一个文本框(txtPassword)和一个按钮(btnLogin)控件,这些控件将由jQueryDialog显示.但是在btnLogin的Click事件中,我无法读取在文本框txtPassword中输入的Text值

I have an ASP.NET page where I have a button When a user clicks on the button,I will check whether the user has logged in or not.If not logged in I will show a modal popup to login (using jQueryUI). I have placed one textbox(txtPassword) and one button(btnLogin) control in the Div which will be shown by the jQueryDialog.But in btnLogin's Click event, I am not able to read the Text value entered in the textbox txtPassword

以下是我的代码

    <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>

服务器端代码

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");
        }
   }

我的Java脚本代码呈现对话框

My java script code to render the dialog

$(function(){

$(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");
 }
});

我已经在ASPX文件的HTML部分中将TextBox1的文本属性值硬编码为'sample'.在单击事件中我得到了它.但是另一个textbox,txtPassword的Text属性却给了我空值

I had hard coded the text properties value of TextBox1 as 'sample' in the HTML part of my ASPX file .In button click event i am getting it.But the other textbox,txtPassword 's Text property is giving me null value

请指导我继续

预先感谢

推荐答案

尝试获取这样的txtPassword值

Try to get txtPassword value like this

string val=txtPassword.Attributes["value"].ToString();

这篇关于带有jQueryUI的ASP.NET:“按钮单击"事件中的文本框值为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆