views:

488

answers:

3

Hey all

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

A: 

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.

Rap
It doesn't look like he's passing in the name of the control. Could you please clarify?
JustLoren
when i view the source, i see this for the textbox element:<input name="ctl00$ContentPlaceHolder1$grdJobEquipment$ctl02$txtQuantity" type="text" value="1.0" id="ctl00_ContentPlaceHolder1_grdJobEquipment_ctl02_txtQuantity" Font-Family="Verdana" Data="182" onkeypress="CheckInputForNumeric(event);" style="font-size:0.9em;width:60px;" />which I don't see anything wrong with...
matthew_360
It's not that there's anything wrong with the HTML, its that you're pointing to it by the name "txtQuantity" when it has been renamed to "ctl00$ContentPlaceHolder1$grdJobEquipment$ctl02$txtQuantity". So there's nothing on your page named txtQuantity anymore. :-(
Rap
A: 

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!

matthew_360
A: 

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);");

Aamir Shahzad