简体   繁体   English

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

[英]change textbox value in client side and read it in server side

I have some textbox and I change the value of this textboxes in clientside (javascript) ,value was changed but when I read in server side after postback actually value not changed.我有一些文本框,我在客户端 (javascript) 中更改了此文本框的值,值已更改,但是当我在回发后在服务器端读取时,实际值未更改。 my textbox isn't read only or disable.我的文本框不是只读或禁用。 notice that I use updatepanel and my postbacks is async.any idea to solve this issue?请注意,我使用 updatepanel 并且我的回发是异步的。有什么想法可以解决这个问题吗?

update I use this jquery to support placeholder in ie,but it cause value of my textboxes equal to placeholder value, and this conflict when my postback is async.更新我使用这个jquery 来支持 ie 中的占位符,但它导致我的文本框的值等于占位符值,并且当我的回发是异步的时会发生这种冲突。 for solving this problem I use below jquery code:为了解决这个问题,我使用下面的 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);

});

I use this code to clear my textbox value before sending to server in add_beginRequest,and set value in add_endRequest (for placeholder in ie).我使用此代码在 add_beginRequest 中发送到服务器之前清除我的文本框值,并在 add_endRequest 中设置值(用于 ie 中的占位符)。 can anyone help solve this problem?任何人都可以帮助解决这个问题吗? thank you.谢谢你。

You changed the value of TextBox with javascript and the respective ViewState is not updated.您使用javascript更改了TextBoxvalue ,并且相应的ViewState未更新。 You can use hidden field to store the value in javascript and get it in code behind.您可以使用hidden字段将值存储在 javascript 中并在后面的代码中获取它。

Html html

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

JavaScript JavaScript

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

Code behind背后的代码

string hdnValue = hdn.Value;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM