I almost tried everything I could to display a nice message box in user control (.ascx) or code behind, but I failed miserably,

Can anyone find me a tutorial in context to what I need and will 100% work please ?

Until now I tried Ajax Modal Popup and JavaScript but it breaks my "visual" webpart without any error, also if you can explain how can I use your suggested tutorial.

.ascx file

<%@ Assembly Name="Uk9999.Webparts.MyWebPart, Version=, Culture=neutral, PublicKeyToken=678d049e0f7e2b28" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyWebPartUserControl.ascx.cs"
    Inherits="UK9999.Webparts.MyWebPart.MyWebPartUserControl" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<div class="MainTable" style="font-family: calibri, verdana, sans serif">
    <div style="width: 100%">
        <div style="float: left; width: 45%; ">

There's more controls and then one update panel which is working fine.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <div align="center" style="color:#*****">
                        <asp:Label ID="lbl1" ....

then in webpart class I am doing this

  public class MyWebpartClass: BaseWebPart
        private const string _ascxPath = @"~/_ControlTemplates/MyWebpartClass/MyWebpartClassUserControl.ascx";

        private UserControl control;
        private ImageButton imagebutton;
        private Button btn;

        protected override void CreateChildControls()

                control = (UserControl)Page.LoadControl(_ascxPath);

                btn = (Button)this.control.FindControl("btnCalculate");
                imagebutton= (ImageButton)this.control.FindControl("btnSendEmail");

                btn.Click += new EventHandler(btn_Click);

Now in

protected void btn_Click(object sender, EventArgs e)
        {// I want to display a message box at the moment its using this.Page.Unload += event for displaying a div which does work but does a page refresh..}

I changed controls names so dont get confused because of that

Now also if I try to add JavaScript in it before first div e.g.

<script runat="server">
   void Page_Load(object sender, EventArgs e) {
           "javascript:alert('ALERT ALERT!!!')");

it displays a blank screen, no errors at all. same thing happens when I try this Ajax Modal Popup. I also tried different other tutorials but non of them actually works.

  • If you want to email the code to my email on profile I can take a look for you, see what you are trying to do, and make a proper suggestion. It all really depends how and when you want the popup. Would love to help.
    – Hugh Wood
    Commented Nov 22, 2012 at 16:16
  • @HughWood would love to do that, but because of company policy I can't pass this code to anyone, however its simply too large to change IDs name and that otherwise I would, however I try to add few bits in my question Commented Nov 22, 2012 at 16:24
  • What do you want the popup to do and say? You could bind a javascript function to the button client side, and do it that way, I can do an example of that if it's suitable.
    – Hugh Wood
    Commented Nov 22, 2012 at 16:38
  • Just a message box, I already tried what your saying but then My webparts shows nothing but blank screen without any errors. Commented Nov 22, 2012 at 16:40
  • Can you create a JSFiddle showing how you adding the popup? Then we can work on it. Maybe copy some of the html involved, and recreate a simpler version there? I finish here in about an hour but I can be on all evening GMT time. JSfiddle.net
    – Hugh Wood
    Commented Nov 22, 2012 at 16:42

2 Answers 2


Why dont you use OnClientClick property of Button? See this MSDN documentation for more information, and also you can use js as you want in ascx


I created a update panel and added my control in it, then using JavaScript I am catching end request of update panel and then displaying a message box, I have been looking for this solution since ages.

 <script type="text/javascript"> 
   // if you use jQuery, you can load them when dom is read.
   $(document).ready(function () {
       var prm = Sys.WebForms.PageRequestManager.getInstance();    


    function InitializeRequest(sender, args) {

    function EndRequest(sender, args) 

now you can display a message box or any other thing you want to do.

Hope it helps :) as I couldn't find any related post or tutorial on internet even after 7 days of search.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.