Can't get Javascript function to run from textbox in gridview

I have a textbox in an ItemTemplate Gridview. In the _RowDataBound event, I add an attribute to the textbox like this:

TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)"); 

      

And it just won't run the JS function.

I tried doing onClick, onBlur, onKeyPress ... even tried to change case to: onclick, onblur, onkeypress ... nothing like it failed to run my JS function.

elsewhere on the same page I have:

    txtAddMarkup.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");

      

(this textarea is missing from the gridview) and it works fine.

I am completely stuck and frustrated at this point because it looks like no matter what I do, I cannot get this textbox to run a JavaScript function

+2


source to share


3 answers


Run the project and look at the name of the textbox generated when viewing the source in a browser (IE, Firefox, Safari, whatever). You will probably see that the name of the textbox has changed. Thanks ASP.



You cannot use the DOM to access elements by name because they are renamed for you.

0


source


For some reason, deleting temporary internet files and reloading the page didn't get the latest .js. I had to unload the project and rebuild it. Which is weird because I've never done this before for other controls.



thanks for your inputs!

0


source


try this:

TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");

txtQuantity.Attributes["onkeypress"] =
    string.Format("javascript:CheckInputForNumeric(this,'{0}','{1}','{2}');", argument1, argument2, argument3);

      

Or simply

txtQuantity.Attributes["onkeypress"] =
    string.Format("javascript:CheckInputForNumeric (this);");

      

0


source







All Articles