?
Solved

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

Posted on 2009-04-24
6
Medium Priority
?
730 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:mainrotor
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Gorkem Yuksel
ID: 24228445
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
 

Author Comment

by:mainrotor
ID: 24228683
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
 
LVL 9

Accepted Solution

by:
Gorkem Yuksel earned 2000 total points
ID: 24228757
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mainrotor
ID: 24237743
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
 
LVL 9

Expert Comment

by:Gorkem Yuksel
ID: 24238297
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
 

Author Closing Comment

by:mainrotor
ID: 31574364
Thank you gyuksel.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question