Question

I have a textbox in the ItemTemplate of a Gridview. On the _RowDataBound event I add an attribute to the textbox like so:

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

And it simply will not fire a JS function.

I've tried doing onClick, onBlur, onKeyPress... even tried changing the case to: onclick, onblur, onkeypress... nothing seems to be able to fire my JS function.

elsewhere on that same page I have:

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

(that textbox is not in a gridview) and this works just fine.

I'm totally stuck and frustrated at this point because it seems no matter what I do, I cannot get this textbox to fire a JavaScript function

Was it helpful?

Solution

Please run your project and look at the name of the textbox generated by viewing the source in the broswer (IE, Firefox, Safari, whatever). You'll likely see that the name of the textbox has changed. Thanks, ASP.

You can't use the DOM to access the elements by name because they're renamed for you.

OTHER TIPS

For some reason, deleting temporary internet files and reloading the page wasn't getting the newest .js. I had to unload the project and re-build it. Which is weird because I've never had to do that before for other controls

thanks for your inputs!

try this one:

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);");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top