0

I have an Update panel where a button and a grid are added as follows: this._UpdatePanel.ContentTemplateContainer.Controls.Add(this._BtnSave); this._UpdatePanel.ContentTemplateContainer.Controls.Add(this._AssignGrid);

On click of this button I am calling a function which resides in a different class AssignedGrid.cs. this._AssignedGrid.getSelectedRows();

AssignedGrid.cs inturn calls a javascript function as follows:

  public void getSelectedRows()
  {
      this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "GetSelectedItems", "<script type='text/javascript' language='javascript'>GetSelectedItems();</script>");    
  }

But this javascript function is never called! Please let me know if I am calling the function the proper way.

5
  • Possible duplicate of stackoverflow.com/questions/1828566/… Commented May 19, 2014 at 15:28
  • I even tried an alert as follows, but did not work: this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "GetSelectedItems", "alert('ok');"); Commented May 19, 2014 at 15:35
  • Does the page use UpdatePanel? Commented May 19, 2014 at 15:36
  • @ Yuriy Galanter yes it has an UpdatePanel. Commented May 19, 2014 at 15:37
  • Then see the answer below. Instead of ClientScript you need to use ScriptManager to call RegisterStartupScript Commented May 19, 2014 at 15:51

2 Answers 2

1

This should work,

ScriptManager.RegisterStartupScrip(this.Page, this.Page.GetType(), "GetSelectedItems", "GetSelectedItems();", true);

The first parameter of the Script manager expects a page object (http://msdn.microsoft.com/en-us/library/bb310408(v=vs.110).aspx)

This is probably a user control, web control, etc and not an actual page object, as such it doesn't work. The only time this works is if you are in the context of a page, e.g. like a class that inherits from page.

So just send it the page object explicitly.

Once you have that, if it's not getting called, use a browser like Chrome, open the developer tools (f12) and go to the console window and look for javascript errors. If it was called but wasn't found you should see "GetSelectedItems does not exist" or a similar error. And that would mean your function is being declared and loaded after the code that calls it was inserted in the dom order by the ScriptManager.

The ScriptManager will insert the code call at the end of the page before the close tag, so define GetSelectedItems in the tag or somewhere above it.

Sign up to request clarification or add additional context in comments.

Comments

1

You dont have to give script tags

ScriptManager.RegisterStartupScript(this, GetType(), "GetSelectedItems", "GetSelectedItems();", true);

2 Comments

@ chandresh patel..Thanks for the response..But did not work!
try this this are the only ways ScriptManager.RegisterClientScript(this, GetType(),"GetSelectedItems", "GetSelectedItems();", true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.