If I want to add external javascript file into an aspx page in back end code page, in what page life should I add it? Page_Load? or Page_PreInit?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As far as I know it doesn’t mind where you put it. I usually put them in the the Page_PreRender event.
There are a couple of reason why I do this.
you want to conditionally add Javascript resources based on some condition in your page, so your need your whole page to be loaded. The PreRender event is the perfect time to check some conditions because everything should be loaded and ready to be renderd.
If you for example add them in the PreInit and in your PageLoad you decide to redirect to a different page you executed unnecessary code.
So even if both points do not apply to the situation I still put them in the PageRender as it’s the guideline for the whole project.