locked
Server Controls not recognized in the external .js codes RRS feed

  • Question

  •  

    For many purposes I have to access the .Net server controls at clientside. I have no problem if the javascript is in the same aspx page but if I have same javascript funtions in separate .js files and try to access those controls, it returns null.

    var

     

    control= $find('<%=lnkExportAll.ClientID %>');

    alert(control)

    Above lines are in the .js file and the alert shows null.

    Can any one tell me what is the problem

    Thursday, November 18, 2010 11:52 AM

All replies

  • For many purposes I have to access the .Net server controls at clientside. I have no problem if the javascript is in the same aspx page but if I have same javascript funtions in separate .js files and try to access those controls, it returns null.

    var

     

    control= $find('<%=lnkExportAll.ClientID %>');

    alert(control)

    Above lines are in the .js file and the alert shows null.

    Can any one tell me what is the problem

    • Merged by Mike Dos Zhang Monday, November 22, 2010 5:44 AM duplicate
    Thursday, November 18, 2010 11:51 AM
  • Hi,

    Most probably your js file is referenced before the controls in your page. So, the js file is getting loaded before the control get loaded. Its trying to find the control which are not present at that moment. So, you can reference your js file after the controls in your page. This may solve your purpose.


    Thanks AnupDG
    Thursday, November 18, 2010 12:24 PM
  • Thankx Anup for your reply. I am sure the thing you said is absolutely correct.

    Can you sugges me the way to access the controls from the external .js file only after the controls are rendered.

     

    Thursday, November 18, 2010 12:28 PM
  • Hi,

    What I have described earlier is correct. But one thing I forgot to mention is that

    <%=lnkExportAll.ClientID %>

    is a C#/VB.net syntax. And you can not execute this syntax in your  js file.

    If you go to the view source of your rendered page, you can find the actual generated id of your controls will be something like ctl00_ContentPlaceHolder1_LinkButton26. A prefix like ctl00_ContentPlaceHolder1_ is generated based on the usage of master page or whether the your controls are inside other controls. In a summary, you need to provide actual Id(rendered) of your control in the js file.


    Thanks AnupDG
    Thursday, November 18, 2010 12:59 PM
  • Hi,

    Most probably your js file is referenced before the controls in your page. So, the js file is getting loaded before the control get loaded. Its trying to find the control which are not present at that moment. So, you can reference your js file after the controls in your page. This may solve your purpose.

    One more thing

    <%=lnkExportAll.ClientID %>

    is a C#/VB.net syntax. And you can not execute this syntax in your  js file.

    If you go to the view source of your rendered page, you can find the actual generated id of your controls will be something like ctl00_ContentPlaceHolder1_LinkButton26. A prefix like ctl00_ContentPlaceHolder1_ is generated based on the usage of master page or whether the your controls are inside other controls. In a summary, you need to provide actual Id(rendered) of your control in the js file.


    Thanks AnupDG
    Thursday, November 18, 2010 3:16 PM
  • Thanks Anup, it solved my problem. I want to ask one more question. I need to access the control of a usercontrol from clientside. Now where should I be writing the javascript funtion for it. Should it be within that ascx(usercontrol) page, or in the aspx(main) page or in the external .js file. I have noticed when i write my javascript in the ascx page, it wont work and if i write in external .js, then it works. Any idea.
    Friday, November 19, 2010 3:48 AM
  • Hi,

    You can run script from anywhere.

    You can do it in the same way above. For accessing a control from script file, you always need to take the actual rendered id of the control. Suppose your control is in UserControl then the id will be something like ctl00_ContentPlaceHolder1_test_LinkButton1 where test is the UserControl id.

     


    Thanks AnupDG
    Friday, November 19, 2010 5:59 AM