I need help displaying a pop-up message in my ASP.Net application with C# code behind.

Hi all,
I get the following error message trying to display a pop-up message in my ASP.Net application.

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

I need to know how to do this.  I have included my code in this post.
CODE BEHIND:
____________
 
MessageBox("Your request has been received and will be reviewed.\r\n" + "Confirmation will be sent to the email address provided.");
 
    private void MessageBox(string msg)
    { 
        Label lbl = new Label();
        lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
        Page.Controls.Add(lbl);
    }

Open in new window

mainrotorAsked:
Who is Participating?
 
Gorkem YukselConnect With a Mentor Commented:
Sorry,

It's friday and I'm not thinking straight..

Change the following line to this:

Page.ClientScript.RegisterClientScriptBlock(GetType(this), "popup", "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>", false);
0
 
Gorkem YukselCommented:
Hi,

You can use the RegisterClientScriptBlock method to run javascript from you C# code.  I've changed your code below to use this method.

Cheers,

G.
MessageBox("Your request has been received and will be reviewed.\r\n" + "Confirmation will be sent to the email address provided.");
 
    private void MessageBox(string msg)
    { 
        Page.ClientScript.RegisterClientScriptBlock(this, "popup", "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>", false);
    }

Open in new window

0
 
mainrotorAuthor Commented:
Hi gyuksel,
I copied your code and I get the following error:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.UI.ClientScriptManager.RegisterClientScriptBlock(System.Type, string, string, bool)' has some invalid arguments
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
mainrotorAuthor Commented:
gyuksel,
I added your code and I got the following error:
Compiler Error Message: CS1501: No overload for method 'GetType' takes '1' arguments

I then removed the word 'this' from the GetType() function and worked fine.  What is the purpose for including 'this' in the function?  
Thank you gyuksel.

mainrotor
0
 
Gorkem YukselCommented:
I applogize again.. LOL.. I wrote the code adhoc with the assumption that it was correct.  Normailly I test my code in VS before submitting it, however this is usually pretty straight forward, so I figured I'd just punch it out.. :o)

I'm glad you were able to figure out the error, and that my posted solution works for you.

Usually in a gettype method, you provide the object from which you wish to obtain the type of.  I meant to use use the function:

typeof(this)

however I wrote it as GetType() instead.. that's what happens on a Friday afternoon I guess.

Cheers,

G.
0
 
mainrotorAuthor Commented:
Thank you gyuksel.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.