1

SCENARIO

The BasePage.cs implements System.Web.UI.Page. It is a superclass without aspx from where all actual pages are derived.

I am registering some javascript in the BasePage using RegisterClientScriptBlock:

public class BasePage : System.Web.UI.Page
{

  protected virtual void Page_Load(object sender, EventArgs e)
  {
    ClientScriptManager cs = Page.ClientScript;
    Type csType = this.GetType();
    cs.RegisterClientScriptBlock(csType, "handler", @"<script language='javascript' type='text/javascript'>alert('Something stupid!');</script>");
  }
}

I tested this snippet in a mock: it works right. Then I inserted the snippet in the BasePage.cs. In the index.aspx the script is not executed. Browser debugging shows that:

  • no script has been registered
  • no console error is displayed

In all other pages it works right.

QUESTION

Is there any specific issue related to the RegisterClientScriptBlock() method?

REMARK

I am sorry, but I am not allowed to post the whole index.aspx page code. But maybe if you have a clue then I can give you further details.

4
  • Where in BasePage is this code? Does the codebehind of index.aspx inherit BasePage or Page? Does the codebehind of index.aspx override the method which calls RegisterClientScriptBlock and not call the base version? Commented Sep 12, 2012 at 15:27
  • @ Rawling: as I said, all the pages inherit from BasePage. I added some context in the code snippet. The page_load method is overridden in the index.aspx page, but the base.Page_Load(sender, e) is executed first. Commented Sep 12, 2012 at 15:31
  • Try taking the virtual out of your Page_Load definition(s) - I think that might be your problem. Commented Sep 12, 2012 at 15:35
  • Does it behaving any differently if moved into the page_init method? Commented Sep 12, 2012 at 17:22

3 Answers 3

1

Eventually a collegue of mine suggested to wrap the index.aspx markup with the form tag. That is enough and now everything is working fine.

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

Comments

0

did you check the option ViewSource in the browser. It will help you findout how the script is written as the final output.

1 Comment

some details added regarding client-side debugging.
0

As I've commented: I believe marking your Page_Load as virtual may mean your descendant pages are somehow overriding it, then not calling the base (Base Page) version, causing the script not to be registered.

2 Comments

I have removed the "virtual" modifier, but nothing changes in the index.aspx. Nice try.
OK. It's very unusual to see virtual there so I thought this was worth a try :)

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.