更改客户端的文本框值并在服务器端读取它

问题描述 投票:1回答:2

我有一些文本框,并且我在客户端(javascript)中更改了此文本框的值,值已更改,但是回发后在服务器端读取时,值实际上未更改。我的文本框不是只读的或禁用的。请注意,我使用updatepanel,而我的回传是async.any解决这个问题的主意吗?

更新我使用this jquery在ie中支持占位符,但是它导致我的文本框的值等于占位符的值,并且当我的回发是异步时会发生这种冲突。为了解决这个问题,我使用下面的jQuery代码:

function EndRequestPostBackForUpdateControls() {
//*****************************For place holder support in ie******************************
if (runPlaceHolder != 0) {
    //alert('end');
    $('input, textarea').placeholder();


    var $inputs = $('.placeholder');
    $inputs.each(function () {

        var $replacement;
        var input = this;
        var $input = $(input);
        var id = this.id;
        if (input.value == '') {
            if (input.type == 'password') {
                if (!$input.data('placeholder-textinput')) {
                    try {
                        $replacement = $input.clone().attr({ 'type': 'text' });
                    } catch (e) {
                        $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
                    }
                    $replacement
                    .removeAttr('name')
                    .data({
                        'placeholder-password': $input,
                        'placeholder-id': id
                    })
                    .bind('focus.placeholder', clearPlaceholder);
                    $input
                    .data({
                        'placeholder-textinput': $replacement,
                        'placeholder-id': id
                    })
                    .before($replacement);
                }
                $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
                // Note: `$input[0] != input` now!
            }
            $input.addClass('placeholder');
            $input[0].value = $input.attr('placeholder');
        } else {
            $input.removeClass('placeholder');
        }
    });
}}
function safeActiveElement() {
// Avoid IE9 `document.activeElement` of death
// https://github.com/mathiasbynens/jquery-placeholder/pull/99
try {
    return document.activeElement;
} catch (err) { }}

function BeginRequestPostBackForUpdateControls() {
//*****************************For place holder support in ie******************************
if (runPlaceHolder != 0) {
    // Clear the placeholder values so they don't get submitted
    var $inputs = $('.placeholder').each(function () {
        var input = this;
        var $input = $(input);
        if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) {
            if ($input.data('placeholder-password')) {

                $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
                // If `clearPlaceholder` was called from `$.valHooks.input.set`
                if (event === true) {
                    return $input[0].value = value;
                }
                $input.focus();
            } else {
                alert($(this)[0].value);
                $(this)[0].value = '';
                alert($(this)[0].value);
                $input.removeClass('placeholder');
                input == safeActiveElement() && input.select();
            }
        }
    });
}}

$(document).ready(function () {

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(BeginRequestPostBackForUpdateControls);

    prm.add_endRequest(EndRequestPostBackForUpdateControls);

});

我使用此代码先清除我的文本框值,然后再在add_beginRequest中将其发送到服务器,并在add_endRequest中设置值(对于ie中的占位符)。谁能帮助解决这个问题?谢谢。

javascript asp.net asynchronous textbox updatepanel
2个回答
3
投票

您用value更改了TextBoxjavascript,并且相应的ViewState未更新。您可以使用hidden字段将值存储在javascript中并在后面的代码中获取。

HTML

<input type="hidden" id="hdn" runat="server" />

JavaScript

document.getElementById("hdn").value = "your value";

后面的代码

string hdnValue = hdn.Value;

0
投票

使用隐藏字段存储值,并在服务器端检索它。

© www.soinside.com 2019 - 2024. All rights reserved.