Saturday, October 22, 2011

Child window (popup) close event in parent window

This is the most useful and most wanted code for raising window close event of child window from parent window.

<script type="text/javascript">
   var childWindow = null;
   var timer;
   var flag = 1;
    
   function openChildWindow() {
       childWindow = window.open('child.htm', 'My popup', 'width=400,height=250');
       childWindow.focus();
   }
   
   function checkChildWindowStatus() {
       if (childWindow.closed) {
           if (flag == 1)
                window.location.reload();
                flag = 0;
           }
           else {
                timer = 0;
                flag = 1;
           }
   }
   
   window.onload = function () {
       timer = setInterval('checkChildWindowStatus()', 1000);
   }
</script>

In the above code childWindow is the variable which reference to the popup window. Using this window status can be identified.


checkChildWindowStatus() is the function where the popup window status is checking and based on this some action (page refresh) can be performed in parent window. This function is getting called for frequent interval of time using serInterval() method.


Happy coding…

InitCap in asp.net

Below is the line of code for rendering text in InitCap format.

System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(txtName.ToString().ToLower())

Here the source string should be in lower case.

Happy coding…

Saturday, October 8, 2011

Access web control on Nested master page from content page

For example I have a DropDownList control in the inner master page. This control can be accessed from the content page in 2 ways.

  1. Using the findcontrol method to access the DropDownList, you need to use the DropDownList's ClientID.

    For example :

    DropDownList list = (DropDownList)this.Master.FindControl("ctl00$ctl00$ContentPlaceHolder1$DropDownList1");

  2. Expose the DropDownList control in the inner master page's code bhind, then the control can accessed directly.
    • Expose the DropDownList control:

        public DropDownList list
        {
             get
             {
                    return this.DropDownList1;
             }
        }

 

  • Add the masterType for the content page to strong type the master page:

    <%@ MasterType VirtualPath="~/Masterpage/NestMasterPage/MasterPage2.master" %>

     

  • Access the DropDownList in the content page's codebehind:

            DropDownList list = this.Master.list;

Happy Coding…