locked
Custom Popup Server Control RRS feed

  • Question

  • User1788567886 posted

    Hi guys, 

    I created a custom server control that is some kind of a popup. You may see the code below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using Core.Common;
    
    namespace Core.Web.Controls
    {
        public class ErrorPopup : Control
        {
            private string errorMessage = "";
    
            public virtual string ErrorMessage
            {
                get { return errorMessage; }
               
                set { errorMessage = value; }
            }
    
            private string redirectPage = "";
            public virtual string RedirectPage
            {
                get { return redirectPage; }
                set { redirectPage = value; }
            }
    
    
    
            protected override void OnPreRender(EventArgs e)
            {
              
                base.OnPreRender(e);
                RegisterJavaScript();
            }
            public void RegisterJavaScript()
            {
                string script =
                   " function OpenErrorPopup(title) {"+
    
                "$('.popup_main_table').attr('width' , '300px');" +
                "$('.pop_data').html('"+ErrorMessage+"');"+
    
                "$('popup_details').fadeIn('fast');" +
                "$('.pop_title').html(title);"+
                "$('.closeBtn').click(function () {"+
                "   $('.popup_details').fadeOut('fast');"+
                  "  $('.pop_data').html('');"+
                  ((!String.IsNullOrEmpty(RedirectPage)) ?   " window.location = '"+RedirectPage+"';" : "")+
                "});}" + "OpenErrorPopup('There is an error!');"
                   ;
    
    
                string ErrorPopup = "<script language=\"javascript\">" + script + "</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorPopup", ErrorPopup);
    
    
            }
        }
    }
    

    n addition I got a master page and a content page. Now here is the important part; I got a div panel for my popup. It actually stays just before the form in master page. If I click a button and make a postback from my content page this popup appears very well on the screen. In order to show my popup, I just use the code below.

                    ErrorPopup p = new ErrorPopup();
                    p.ErrorMessage = ex.Message;
                    Page.Controls.Add(p);
    
    

    However, the problem is when I put popup's div panel as a child of form tag, the code above is not working, and popup is not shown on the screen. I wonder what the differences between putting before form tag or putting after form tag. By the way popup div is simply:

    <div class="popup_details" style="display:none;">
            <table cellpadding="0" align="center" class="popup_main_table" width="700px" cellspacing="0"
                border="0">
                <tr>
                    <td class="popup_top_left">
                    </td>
                    <td valign="top" class="popup_top_center">
                        <div class="popup_heading">
                            <h1>
                                <asp:Label runat="server" ID="lbl_AddToCart" class="pop_title" Text="Sepete Ekle"
                                    meta:resourcekey="lbl_AddToCartResource1"></asp:Label></h1>
                        </div>
                        <div class="pop_close">
                            <a class="closeBtn"></a>
                        </div>
                    </td>
                    <td class="popup_top_right">
                    </td>
                </tr>
                <tr>
                    <td class="pop_detail_white_bg">
                    </td>
                    <td class="pop_detail_white_bg">
                        <div class="pop_data">
                        </div>
                    </td>
                    <td class="pop_detail_white_bg">
                    </td>
                </tr>
                <tr>
                    <td class="popup_bottom_left">
                    </td>
                    <td class="pop_detail_white_bg">
                    </td>
                    <td class="popup_bottom_right">
                    </td>
                </tr>
            </table>
        </div>

    Thanks for your help,
    Regards, Kemal.

    Saturday, July 23, 2011 8:51 PM

Answers

  • User3866881 posted

    Hello OP:)

     I think it's reason of "RegisterClientScriptBlock"——

    This method will create a series of js codes and put them at the head of your page. However, if you load the js too much earlier than the server-side controls is succwssfully initialized, maybe the js won't be executed very well, which causes the problem.

    Compared with this, I think you can try to use——RegisterStartupScript

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 25, 2011 9:17 PM