Click here to Skip to main content
16,007,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can do it at c# like this:
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      //some script
   }
}


i have countries.js and need to call the function inside this file, the function is to list all the countries into <Select> drop down list.

normally i paste this under content place holder,

HTML
<script language="javascript">
print_country('ctl00_ContentPlaceHolder1_country', 'ctl00_ContentPlaceHolder1_state');
</script> 
the script do functioning normally, but it always return selectedIndex as '0', so i wonder if I put the script under !Page.IsPostback ..
Posted

Remove the javascript code from content placeholder and register it inside !Page.IsPOstBack. This will make it to render only when page loads for the first time.

XML
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {

string jScript;
    jScript="<script>print_country('ctl00_ContentPlaceHolder1_country', 'ctl00_ContentPlaceHolder1_state');</script>";
    Page.RegisterClientScriptBlock("keyClientBlock",jScript);
  }
}
 
Share this answer
 
Comments
melvintcs 30-Dec-11 5:49am    
seems like it cant find my 'ctl00_ContentPlaceHolder1_country', any idea?
i double confirm, the ID is correct..
saini arun 30-Dec-11 6:20am    
What do you mean by can't find the control? What error does it generate?
[You can use clientID to get the id of the control rendered on the page.]

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

string jScript;
jScript="<script>print_country('"+country.ClientID+"', '"+state.ClientID"');</script>";
Page.RegisterClientScriptBlock("keyClientBlock",jScript);
}
}
melvintcs 30-Dec-11 6:46am    
error with " at ur code
i did this and it's still not working...
protected void Page_Load(object sender, EventArgs e)
{
string jScript;
jScript = "<script>document.getElementById('<%=Label1.ClientID%>').innerHTML = 'testing';</script>";
Page.RegisterClientScriptBlock("keyClientBlock", jScript);
}
Hi,
check following link,
hope your get the solution..
http://www.tedpavlic.com/post_detect_refresh_with_javascript.php[^]
 
Share this answer
 
C#
protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {

string str="print_country('ctl00_ContentPlaceHolder1_country', 'ctl00_ContentPlaceHolder1_state');";
    Page.ClientScript.RegisterClientScriptBlock(this.getType(), '%^&$%', str,true);
  }
}
 
Share this answer
 
Comments
melvintcs 30-Dec-11 5:35am    
what is '%^&$%'? it's not working...

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