Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to access javascript from a aspx page having master page using below code in aspx page load :

HtmlGenericControl toAddJavaScript = new HtmlGenericControl();
toAddJavaScript.TagName = "script";
toAddJavaScript.Attributes.Add("type","text/javascript");
toAddJavaScript.Attributes.Add("src","js/CommonInformationScript.js");
this.Page.Header.Controls.Add(toAddJavaScript);
textCustomerName.Attributes.Add("OnKeyUp","return GetCustomers());

This code is hitting GetCustomers() method of javascript successfully.In this method I am trying to get value of text box name "textCustomerName" (server control)using below code :

var customerSubString = document.getElementById("textCustomerName").value;

But this line is creating some error and there is no value coming in customerSubString variable.

please guide me.

Thanks
Divya
Posted
Updated 28-Jan-10 7:45am
v2

XML
var customerSubString=document.getElementById('<%=textCustomerName.ClientID%>')

It gave me null value in variable customerSubString but it works if I use complete id "ctl00_ContentPlaceHolder1_textCustomerName" in place of using ClientId Property.


I assume this means you did it in your js file, like I told you not to. It's not being evaluated.
 
Share this answer
 
You have a fundamental problem, and it has nothing to do with master pages.


Divymital wrote:
= document.getElementById("textCustomerName").value;


The name you assign on the server side, is not the name used on the client side. The control has a ClientID property, and you need to access that on the server and insert it into your javascript. This means that bit of script cannot live in a js file. The way I manage this is for my js file to access an id via a variable called something like clientSideCustomerNameId. Then my code behind emits the js to create that variable with the right value.
 
Share this answer
 
Hi,

Thanks for your reply.

When I used code:

var customerSubString=document.getElementById('<%=textCustomerName.ClientID%>')

It gave me null value in variable customerSubString but it works if I use complete id "ctl00_ContentPlaceHolder1_textCustomerName" in place of using ClientId Property.

Regards,
Divya
 
Share this answer
 
Thanks, this worked and I am able to get the value od text box in Java Script.
 
Share this answer
 
Hi,

In the Code behind pass the Client Id of the Texbox in your Javascript function as below:

textCustomerName.Attributes.Add(GetCustomers('"+textCustomerName.ClientID+"'))


and in the Javascipt function use that id.

var customerSubString = document.getElementById(parameterName).value;


Replace the 'parameterName' with the one put in your function.

Regards,
Ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900