0

i am using this script in .cs page..

public void messagebox(string msg)
{
  Label lbl = new Label();
  lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
  Page.Controls.Add(lbl);
}

error as

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

3
  • 2
    You should accept more answers on your previous questions before you proceed to ask more. Commented May 4, 2011 at 13:42
  • 1
    Initially I looked and saw many un-answered questions, but then out of 24 questions a LOT have been answered and at least some deserve an accept Commented May 4, 2011 at 13:46
  • possible duplicate of Javascript Alert in ASP.NET Commented May 4, 2011 at 13:54

3 Answers 3

1

To register a script, use ScriptManager

ScriptManager.RegisterStartupScript(this, this.GetType(), "registeredAlert", lbl, false);
Sign up to request clarification or add additional context in comments.

Comments

0

This has nothing to do with Javascript. The error message is pretty clear; you're not allowed to add any more controls to Page.Controls at that point in your code.

Comments

0

To register a script on the page use RegisterStartupScript

ScriptManager.RegisterStartupScript Method (Control, Type, String, String, Boolean)

If you are using an update panel you have to call the first two parameters with the updatepanel control

ScriptManager.RegisterStartupScript(updatePanel,
    updatePanel.GetType(),
    "key",            // unique key means it will never insert the same script twice
    "alert('hi');",   // javascript
    true);            // include <script> tags

Comments

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.