0

I have a TextBox on an ASP.Net page with OnTextChanged set to a code-behind 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:

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.

Note that the TextBox is in an HTML table within an ASP.Net View control. Also, there are a multitude of javascript functions on this page that work fine. This issue only arises when I try to add the function above. The project is using .Net Framework 4, and I'm working in Visual Studio 2017. What the heck is going on here?

0

1 Answer 1

1

This might not solve the entire problem but these statements are not valid 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.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.