为什么文本框的值在回发后变为空?

时间:2012-10-03 10:34:18

标签: jquery asp.net button textbox popup

我有一个div,我在jquery模式弹出中显示。我里面有一个文本框和一个按钮。点击该按钮,我发送代码背后的请求。现在,当我试图获取文本框的值时,每次都会出现“”。下面是我的HTML。

<div id="divDelete" runat="server" style="display: none;">
        <div>
            <div>
                <h2>
                    Are you sure you want to delete your wishisdone account?</h2>
                <p>
                    Is there anything that can make you reconsider? Contact our support, if you have
                    any problems and we will help you.</p>
                <p>
                    If you wish to continue, and delete your account, be aware that we will only keep
                    your files for 30 days and then they will be permanently deleted. You can reactivate
                    your account at any time within 30 days by logging back in.
                </p>
                <p>
                    Please, let us know the reason for your decision, so we can make our services more
                    convenient to use.</p>
                <br />
                <label class="blackLabel">
                    Password&nbsp;<span class="mandatory">*</span></label>
                <p>
                    <asp:TextBox ID="txtPassword" CssClass="greyInputLarge" runat="server"></asp:TextBox>
                    <br clear="all" />
                    <span class="error" id="spPassword" runat="server"></span>
                </p>
                <label class="blackLabel">
                    Why are you leaving?&nbsp;</label>
                <p>
                    <asp:DropDownList ID="drpReasonOfDelete" CssClass="greySelectLarge" runat="server">
                        <asp:ListItem Text="I have another WishIsDone account" Value="1"></asp:ListItem>
                        <asp:ListItem Text="I haven't registered this account" Value="2"></asp:ListItem>
                        <asp:ListItem Text="No need in service anymore" Value="3"></asp:ListItem>
                        <asp:ListItem Text="Technical problems" Value="4"></asp:ListItem>
                        <asp:ListItem Text="WishIsDone is too slow for me" Value="5"></asp:ListItem>
                        <asp:ListItem Text="Some features do not work" Value="6"></asp:ListItem>
                        <asp:ListItem Text="I don't understand how to use WishIsDone" Value="7"></asp:ListItem>
                        <asp:ListItem Text="Other" Value="8"></asp:ListItem>
                    </asp:DropDownList>
                </p>
                <label class="blackLabel">
                    Please, explain further&nbsp;</label>
                <p>
                    <asp:TextBox ID="txtReason" CssClass="greyInputLargeForMultiline" runat="server"
                        TextMode="MultiLine"></asp:TextBox>
                </p>
                <asp:HiddenField ID="hdfPassword" runat="server" />
                <br clear="all" />
            </div>
        </div>
    </div>

我的jquery文件是

$(document).ready(function () {
var $Error1 = $('[id$=divDelete]').dialog({
    autoOpen: false,
    resizable: false,
    modal: true,
    height: 'auto',
    width: 580,
    title: "Deactivate Account",
    buttons: {
        "Close": function () {
            $Error1.dialog('close');
        },
        "Delete my account": function () {
            if ($('[id$=txtPassword]').val() == '') {
                $('[id$=txtPassword]').css('border', '1px solid red');
                $('[id$=spPassword]').text("You can't leave this empty.");
                return false;
            } else {
                var password = $('[id$=txtPassword]').val();

                $('[id$=hdfPassword]').val(password);
                alert($('[id$=hdfPassword]').val());
                $('[id$=Button1]').click();
            }
        }
    },
    close: function (event, ui) {
    },
    open: function () {
    }
});

$('[id$=btnOpenPopUp]').live('click', function (e) {
    e.preventDefault();
    openPopUp();
});

function openPopUp() {
    $Error1.dialog('open');
}

var querystring = GetQueryStringParams('delete');
if (querystring == '1') {
    openPopUp();
}
});
function GetQueryStringParams(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
    var sParameterName = sURLVariables[i].split('=');
    if (sParameterName[0] == sParam) {
        return sParameterName[1];
    }
}

}

代码背后的代码是

protected void Button1_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(Request.QueryString["delete"]) && !string.IsNullOrEmpty(Request.QueryString["u"]))
    {
        try
        {
            Guid userId = new Guid(Convert.ToString(Request.QueryString["u"].ToString()));
            objUsers.Id = userId;
            objUsers.Status = "T";
            objUsers.ModifiedOn = DateTime.UtcNow;
            string password = txtPassword.Text;
            string newass = hdfPassword.Value;
                       }
        catch
        {
            Response.Write("There is some error while deleting the account.");
        }

    }
}

请帮助我,当我试图通过alert()获取它时,它会在文本框中返回输入的值。但在它背后的代码来自“”。我很困惑为什么会发生这种情况?请帮助我。

1 个答案:

答案 0 :(得分:4)

从我的头脑中,我认为这是因为在使用jQuery UI Dialog时,它实际上需要<div>的内容,然后将其附加到body的{​​{1}} }。

因此,html代码现在位于<input>代码之外。

因此,在<form>上,控件实际上并未在请求的内容中发布。

以下是该问题的一些解决方案。

Input inside Jquery UI Dialog not being sent?

jQuery UI Dialog with ASP.NET button postback