当文本框的值jQuery的code变更不会触发asp.net文本框更改事件 [英] asp.net textbox changed event not fired when textbox value is changed in jquery code

查看:153
本文介绍了当文本框的值jQuery的code变更不会触发asp.net文本框更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框,名字txtFirstName,姓txtLastName和AD用户ID txtADUserID。当第一个名字或姓氏改变了,我分配用户价值jQuery中code AD ID。我看到在浏览器ADUserID文本框的值被改变。

问题:我需要检查,如果用户ID在txtADUserID的ontextchanged服务器端事件已经存在。 OnTextChanged事件是不是在所有发射和回传也没有提高。

ASP.net code

 < ASP:文本框ID =txtADUserID=服务器MAXLENGTH =255的ClientIDMode =静态OnTextChanged =txtADUserID_TextChanged的AutoPostBack =真>&LT ; / ASP:文本框>

jQuery的code:

  $(文件)。就绪(函数(){
        $(#txtFirstName)。在('变化',函数(){
           $('#txtADUserID')VAL($('#txtFirstName')VAL()的charAt(0)+ $('#txtLastName')VAL()。)。
        });
        $(#txtLastName)。在('变化',函数(){
           $('#txtADUserID')VAL($('#txtFirstName')VAL()的charAt(0)+ $('#txtLastName')VAL()。)。
        });
    });

背后code code:

 保护无效txtADUserID_TextChanged(对象发件人,EventArgs的发送)
            {
//引发一个错误,如果用户已经存在
         }


解决方案

您正在改变通过JavaScript无需用户交互的文本,这就是为什么你没有得到服务器端回发。您可以添加按钮和绑定的服务器端事件到该按钮并执行按钮点击当你需要回发。还存储文本在某些隐藏字段作为的你不会得到更新文本值在服务器端。

  $(文件)。就绪(函数(){
      $(#txtFirstName)。在('变化',函数(){
           $('#txtADUserID')VAL($('#txtFirstName')VAL()的charAt(0)+ $('#txtLastName')VAL()。)。
           $('#buttonId')点击()。
      });
      $(#txtLastName)。在('变化',函数(){
           $('#txtADUserID')VAL($('#txtFirstName')VAL()的charAt(0)+ $('#txtLastName')VAL()。)。
           $('#buttonId')点击()。
      });
});

I have three text boxes, first name txtFirstName, last name txtLastName and AD User ID txtADUserID. Whenever first name or last name is changed, I am assigning AD user id value in Jquery code. I see in browser that ADUserID textbox value is changed.

PROBLEM: I need to check if user id already exist in txtADUserID's ontextchanged server side event. OnTextChanged event is NOT fired at all and postback is also not raised.

ASP.net code

<asp:TextBox ID="txtADUserID" runat="server" MaxLength="255" ClientIDMode="Static" OnTextChanged="txtADUserID_TextChanged" AutoPostBack="true"></asp:TextBox>

Jquery code:

 $(document).ready(function () {
        $("#txtFirstName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
        });
        $("#txtLastName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
        });
    });

Code behind code:

    protected void txtADUserID_TextChanged(object sender, EventArgs e)
            {
// Raise an error if user already exist
         }

解决方案

You are changing text through javascript without user interaction that is why you did not get server side postback. You can add a button and bind server side event to that button and perform button click when you need postback. Also store the value of textbox in some hidden field as you will not get the updated textbox value on server side.

$(document).ready(function () {
      $("#txtFirstName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
           $('#buttonId').click();  
      });
      $("#txtLastName").on('change', function () {
           $('#txtADUserID').val($('#txtFirstName').val().charAt(0) + $('#txtLastName').val());
           $('#buttonId').click();
      });
});

这篇关于当文本框的值jQuery的code变更不会触发asp.net文本框更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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