简体   繁体   English

当 javascript function 添加到 web 页面时,ASP.Net OnTextChanged 事件不会触发

[英]ASP.Net OnTextChanged event doesn't fire when javascript function added to web page

I have a TextBox on an ASP.Net page with OnTextChanged set to a code-behind function:我在 ASP.Net 页面上有一个 TextBox,其中 OnTextChanged 设置为代码隐藏 function:

<asp:TextBox ID="txtFirstName" runat="server" Width="150px"
    OnTextChanged="txtFirstName_TextChanged" AutoPostBack="true"></asp:TextBox>

protected void txtFirstName_TextChanged(object sender, EventArgs e)
{
    if (cbExisting.Checked)
    {
        DataSet ds = 
            DataGateway.getExistingEmployeeInfo(txtLastName.Text, txtFirstName.Text);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows.Count == 1)
                txtEmployeeID.Text = ds.Tables[0].Rows[0]["EmployeeID"].ToString();
            else
            {
                lblError.Text = "There is more than one existing employee with that name.<br>";
                lblError.Text = lblError.Text + "Please indicate which employee you want.<br>";
                foreach (DataRow drow in ds.Tables[0].Rows)
                {
                    TableRow trow = new TableRow();
                    TableCell cell = new TableCell();
                    CheckBox cb = new CheckBox();
                    cb.Text = drow["FirstName"].ToString() + " " + 
                        drow["LastName"].ToString() + " (" + 
                        drow["EmployeeID"].ToString() + ")";
                    cb.AutoPostBack = true;
                    cb.EnableViewState = true;
                    cb.ViewStateMode = ViewStateMode.Enabled;
                    cb.Attributes.Add("onclick", "populateEmpID('"+ 
                        cb.ClientID + "','" + 
                        txtEmployeeID.ClientID + "');");
                    cell.Controls.Add(cb);
                    trow.Cells.Add(cell);
                    tblMultEmps.Rows.Add(trow);
                }
            }
        }
    }
}

This all works just fine UNTIL I add the javascript function referred to in my code to the web page:这一切都很好,直到我将我的代码中提到的 javascript function 添加到 web 页面:

function populateEmpID(CheckBoxID, EmployeeID) {
        var cb = document.getElementById(CheckBoxID);
        var tbempid = document.getElementById(EmployeeID);
        int cblen = cb.length;
        String empid = cb.value.substring(cblen - 6, cblen - 1);
        tbempid.value = empid;
    }

Once I add this javascript function, the OnTextChanged event stops firing.一旦我添加了这个 javascript function,OnTextChanged 事件就会停止触发。

Note that the TextBox is in an HTML table within an ASP.Net View control.请注意,TextBox 位于 ASP.Net 视图控件内的 HTML 表中。 Also, there are a multitude of javascript functions on this page that work fine.此外,此页面上有许多 javascript 功能可以正常工作。 This issue only arises when I try to add the function above.仅当我尝试添加上面的 function 时才会出现此问题。 The project is using.Net Framework 4, and I'm working in Visual Studio 2017. What the heck is going on here?该项目正在使用.Net Framework 4,我在 Visual Studio 2017 中工作。这到底是怎么回事?

This might not solve the entire problem but these statements are not valid js:这可能无法解决整个问题,但这些语句不是有效的 js:

int cblen = cb.length;
String empid = cb.value.substring(cblen - 6, cblen - 1);

Should be应该

var cblen = cb.length;
var empid = cb.value.substring(cblen - 6, cblen - 1);

Again, there could be other issues.同样,可能还有其他问题。

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

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