This is a migrated thread and some comments may be shown as answers.

Closing RadWindow with UserControl as ContentTemplate on Button click

2 Answers 343 Views
Window
This is a migrated thread and some comments may be shown as answers.
Mojeeb
Top achievements
Rank 1
Mojeeb asked on 24 Mar 2011, 09:13 AM
Hi,

I am using RadWindow as ContentTemplate and in the Content Template I am using a Custom UserControl to do some functionality. In my Default Page, I have a RadWindow which also has a UserControl as ContentTemplate. When this window Opens the user can click a button to open another RadWindow with UserControl as Content Template. Now in this User Control I do some things and then I want to close the window. I can register a startup script from server side to call a CloseWindow javascript function. But the problem is that I can't get the reference of the RadWindow using the GetRadWindow() function, which is used on the content pages to get the reference of the radWindow. But this function does not give me the reference to the Rad window. 
Here is a sample of how i am using most of my RadWindows:
<telerik:RadWindow ID="rw_EditObjectRecordScreen" runat="server" Modal="true" Behaviors="Close, Move, Resize  "
    Width="900px" Height="500px" Visible="true"
    OnClientClose="CloseEditObjectRecordWindowODR"
    DestroyOnClose="true" >
    <ContentTemplate>
        <uc1:EditObjectRecord ID="EditObjectRecord1" runat="server" />
    </ContentTemplate>
</telerik:RadWindow>

I am not using any ScriptManager or AjaxManager, or RadWindowManager on any of my pages. I only use AjaxPanel to wrap all my user Controls. then I use these User Controls in the RadWindow's ContentTemplate. and i open the window from server side by setting the Visible on Page Load property to TRUE.
Is there a way to Close the RadWindow, which is used as a Content Template and the Content is a User Control, from with in the UserControl Code? I have tried GetRadWindow() method but it does not work. then I also tried the following:
function GetContentRadWindow() {
                //using ContentTemplate instead of Navigate URL
                //for RadWindow so we need to get RadWindow by name
                var oWnd = GetRadWindowManager().getWindowByName("RadContentWindow");
                return oWnd;
            }
 but as I am not using any WindowManager so this also did not work. and This code is run on the Page where the RadWindow is defined and not inside the Content.

Any help will be appriciated.

/MO

2 Answers, 1 is accepted

Sort by
0
Accepted
Svetlina Anati
Telerik team
answered on 25 Mar 2011, 04:03 PM
Hello Mojeeb,

 I already answered your other thread and for your convenience and for other show might have the same question I pasted my reply below:

As far as I understand you have multiple RadWindows which are only used with ContentTemplate and user controls inside those RadWindows from which you want to manipulate the RadWindows.

If so, what I can suggest is the following:

1) Put a function on the main page which does what you want with the RadWindow you want. This function can reference the RadWindow by using $find since they are on the same page. If you do not want to use $find, you can use global javascript variables and assign them to the sender in the OnClientShow event (this will be the RadWindow you open). No matter how you decide to reference the RadWindows, all you need there is to declare the function which manipulates them and which should be executed from the user controls.

2) I assume that the code you have tried is the one with the syntax GetRadWindow().BrowserWindow - it will throw errors because this is used when there is another page through an url set in the RadWindow and in your case you have ContentTemplate. Having ContentTemplate means that all the RadWindow itself and its content are on the same page, so you will have the function declared in 1) on the same page and you can directly call it. However, since you say that you use the same controls on other pages, they might not need this function - that is why before directly calling it from the user control you should check if 1) there is such variable and 2) this variable is of type function. Once you check this, you can simply call the function, no need to reference parent windows because you have everything in the same frame tree hierarchy.

I hope that my reply is helpful, let me know how it goes.

Best wishes,
Svetlina
the Telerik team
0
Mojeeb
Top achievements
Rank 1
answered on 06 Apr 2011, 09:33 AM
Hi Svetlina,
Thanks for the reply. I tried your suggestions and i worked really well. Let me explain a bit for you and others what I was trying to do.

I had a RadGrid in a UserControl (I will call this file radGridUserControl.ascx). I use this user control in DotNetNuke site's Page. So when i click the page i see the UserControl which displays the RadGrid with some data. I have created a settings page to be used with this usercontrol so that based on the settings i display different data in the RadGrid. This way i can use One UserControl to display the data from different Tables from DB. So i put this same RadGrid Usercontrol on the same DNN page multiple times andchange the settings on each control do display different data in the grid.
In the RadGrid, i have Add/Edit functionality. But instead of opening the Edit form in the Grid i was opening the form in a RadWindow. The reason for this is beacuse based on the Settings i have to display different Edit forms for different data grids. So i created another user control for dynamic edit form based on settings. i declared my RadWindow in the radGridUserControl.ascx page and in its content template I declared the usercontrol.
<telerik:RadWindow ID="rw_DynamicEditForm" runat="server" Modal="true" Behaviors="Close, Move, Resize"
    Width="350px" Height="420px"
    DestroyOnClose="true" VisibleStatusbar="False" >
    <contenttemplate>
            <uc1:DynamicEditForm  ID="DynamicEditForm1" runat="server" />
    </contenttemplate>
</telerik:RadWindow>

Now when i press the Add or Edit link, i can open this RadWindow and display a dynamically generated Edit Form. The user can edit stuff and save the information.
In this Edit form i have a LookUp field, which works like this: the lookup field is a text box with a link button infront of it. the user can click this link button and another radwindow will open and user can see a list of data and select one value from that list and close the radwindow and the selected value will be written in the text box.
What I needed was that the second radwindow for lookup should automatically be closed after the user has selected a record in lookup rad window.
so the solution provided by Svetlina worked for me. as you said, that if content template is used and a user control is used in the content template of the radwindow, it means that all the code from the parent as well as the usercontrol will be on the same page. so I wrote a java script function in the parent where i have defined the radwindow which gets the reference of the radwindow and call its close method. The next big issue for me was, how and when to call this method. i dont want the user to press a close button which will call this javascript function. I wanted to close the radwindow after the user has selected a record. In order to achieve this functionality i used this approach Executing custom javascript code after AJAX update. After the user clicks a record in the Lookup radWindow, in hte server side i capture that select event and then registered the script which calls the CloseWindow javascript function. And it worked really good.
Now for the Third issue, i have to make these usercontrols reusable, so that they can be used multiple times on the same page or different pages or in other radwindows or how ever they are used the functionality should work.
So here is what i did next to achieve that. In my Lookup User control i created a new Event for RecordSelection. in the user control all i do is display the data and handle the Dataselection event. where ever the LookUp user control is used, i hook the RecordSelection custom event, and in this event i register a script to call the close window javascript function. another thing that i did is to write the javascript functions in the server side in page load event with dynamic function names so that if a control is used multiple time on the same page, the javascript functions' names don't match.
string script = "";
script += " \r\n";
script += "<script type=\"text/javascript\"> \r\n";
script += "//<![CDATA[ \r\n";
script += "         function " + rw_LookUpScreen.OnClientClose + "(sender, args) { \r\n";
script += "             var ajaxManager = $find(\"" + rapEditObjectRecord.ClientID + "\"); \r\n";
script += "             ajaxManager.ajaxRequest('CloseLookUpWindowEOR'); \r\n";
script += "         } \r\n";
script += "         function GetTheLookUpRadWindow" + this.ClientID + "() { \r\n";
script += "            //using ContentTemplate instead of Navigate URL \r\n";
script += "            //for RadWindow so we need to get RadWindow by ClientID \r\n";
script += "            var oWnd = $find(\""+ rw_LookUpScreen.ClientID + "\"); \r\n";
script += "            return oWnd; \r\n";
script += "         } \r\n";
script += " \r\n";
script += "         function AutoCloseLookUpWindow" + this.ClientID + "() { \r\n";
script += "            //create the argument that will be returned to the parent page \r\n";
script += "            var oArg = new Object(); \r\n";
script += "            var oWnd = GetTheLookUpRadWindow" + this.ClientID + "(); \r\n";
script += " \r\n";
script += "            if (oWnd != null) { \r\n";
script += "                //Close the RadWindow and send the argument to the parent page \r\n";
script += "                oWnd.close(oArg); \r\n";
script += "            } \r\n";
script += "         } \r\n";
script += "    /* ]]> */ \r\n";
script += "</script> \r\n";
 
 
// WRITE THE SCRIPT TO CLOSE RAD WINDOW
ScriptManager.RegisterStartupScript(this, GetType(), "WinCloseScript_" + this.ClientID, script, false);
This code is written in the PageLoad of the parent on which i am creating the LookupRad window. so the parent is responsible for handling the radWindow not the child. So this way the Lookup Usercontrol can be used with or without radWindow.




Tags
Window
Asked by
Mojeeb
Top achievements
Rank 1
Answers by
Svetlina Anati
Telerik team
Mojeeb
Top achievements
Rank 1
Share this question
or