Question passing asp.net textbox id to external JavaScript function

tomhil

Member
Joined
Jan 23, 2012
Messages
6
Programming Experience
1-3
Hello everyone,

I'm trying to pass a textbox id as a parameter to an external javascript file but its not working.
here is my textbox
VB.NET:
 <asp:TextBox runat="server" ID="txt_merchant_number" CssClass="txtFlat" onclick="check_by_number('<%= this.by_number.ClientID %>')"
                                        ClientIDMode="Static"></asp:TextBox>

and here is the function in my external javascript file

VB.NET:
function check_by_number(val) {
    document.getElementById(val).checked = true;
}

basically i'm trying to check the radio button if the textbox is clicked on. I've tried adding attributes to my textbox on pageload but no success. Any help would be greatly appreciated thanks.
 
Embedded code in server tag attributes is not supported, it would work for regular markup tags.
Also, not to confuse with Javascript, your "this.by_number" should of course be "Me.by_number" in VB.Net code.
For server tags you can use code behind and add the attribute there:
Me.txt_merchant_number.Attributes.Add("onclick", String.Format("check_by_number('{0}')", Me.by_number.ClientID))
 
Embedded code in server tag attributes is not supported, it would work for regular markup tags.
Also, not to confuse with Javascript, your "this.by_number" should of course be "Me.by_number" in VB.Net code.
For server tags you can use code behind and add the attribute there:
Me.txt_merchant_number.Attributes.Add("onclick", String.Format("check_by_number('{0}')", Me.by_number.ClientID))

Thank you for the reply. I added the attribute in page_load changed my function call to
VB.NET:
onclick="check_by_number('<%= Me.by_number.ClientID %>')"

but i am getting null or undefined error "Microsoft JScript runtime error: The value of the property 'check_by_number' is null or undefined, not a Function object".
 
It would be a good idea to put the code I posted in Page_Load handler. Tested and worked fine as it should.
 
If you pasted the exact code I posted in Load handler I see no reason why you would get any error if the function was defined and available. How and where have you imported the script?
 
Back
Top