I'm trying to use a RadButton to close a radwindow from with the window itself (via javascript). Is it possible to call a script to close the window? Here is the javascript:
function getRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function closeWindow()
{
getRadWindow().close();
}
And here is the button:
<telerik:RadButton ID="CancelButton" runat="server" OnClick="closeWindow();" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">
</telerik:RadButton>
I have tried everything, the script will only work if I use a pure HTML element such as an anchor tag. If I use the OnClick event I get the following error when the window opens: Compiler Error Message: CS1026: ) expected.
Am I missing something?
Thanks!
I'm not sure if I am improving this answer, I'm just trying to make it easier to understand. I have a rad window that is opened from a main page. The radwindow is opened in Code Behind (C#), not Javascript. When my user clicks a Save button on the RadWindow, it performs some logic tasks, then it closes the radwindow itself. You simply need to:
Put thise code block in you RadWindow aspx.....
<telerik:RadCodeBlock runat="server" ID="rcb1">
<script language="javascript" type="text/javascript">
function GetRadWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function CloseDialog(button)
{
GetRadWindow().close();
}
</script>
</telerik:RadCodeBlock>
Put this code in your RadWindow's button click after you perform your pre-close logic (the same button that performs the other logic closes the window)
C#
ClientScript.RegisterStartupScript(typeof(string), "", "CloseDialog();");
OR
VB
ClientScript.RegisterStartupScript(Me.GetType(), "", "CloseDialog();")
If you're wondering how to open the radwindow from codebehind here is how I did it:
RadWindow window1 = new RadWindow();
// Set the window properties
window1.NavigateUrl = "winStrengthChart.aspx?EMPLOYIDNAME=" + parmString;
window1.ID = "RadWindow1";
window1.Width = 800;
window1.Height = 650;
window1.VisibleStatusbar = false;
window1.Behaviors = Telerik.Web.UI.WindowBehaviors.Close | Telerik.Web.UI.WindowBehaviors.Resize | Telerik.Web.UI.WindowBehaviors.Move;
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code
rwm1.Windows.Add(window1);
this.Form1.Controls.Add(window1);
...AND of course you need a basic RadWindowManager on the main page that opens the window:
<telerik:RadWindowManager ID="rwm1" runat="server">
<Windows>
</Windows>
</telerik:RadWindowManager>
This should work if I have made a mistake please correct me.
Thanks
The way to call a function from the RadButton is by using either its OnClientClicked or OnClientClicking event. Then you need to pass only the name of the JavaScript function, without parenthese. OnClick is a property for the server handler, this is also the case with the regular asp button. Try this:
<telerik:RadButton ID="CancelButton" runat="server" OnClientClicked="closeWindow" AutoPostBack="false" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">
Note the AutoPostBack property is set to false to prevent the postback.
Related
I am using the following JS/JQuery code in order to close the browser at a particular event.
It works perfectly on Internet explorer but not sure why its not working on Google Chrome.
<div class="divRowButton">
<asp:LinkButton ID="btnContinue" runat="server" CssClass="button buttonText" Width="205px"
Height="33px" CausesValidation="false" OnClientClick ="return CloseBrowser();"></asp:LinkButton>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
//IE6 Fix, expand the content to full height
if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6) {
$divH = $("#secHeader");
$divN = $("#secNav");
$divF = $("#secFooter");
var ht = $(window).height() - $divH.height() - $divN.height() - $divF.height();
$(".divContent").height(ht - 10);
}
});
function CloseBrowser() {
window.open('', '_self', '');
window.close();
}
</script>
</asp:Content>
I changed my above code to the following below code but still it didn't work for chrome. I am wondering what changes do I need to make in the below code so that it works perfectly for chrome.
<div class="divRowButton">
<asp:LinkButton ID="btnContinue" runat="server" CssClass="button buttonText" Width="205px"
Height="33px" CausesValidation="false" OnClientClick ="return CloseBrowser('quit');"></asp:LinkButton>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
//IE6 Fix, expand the content to full height
if (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6) {
$divH = $("#secHeader");
$divN = $("#secNav");
$divF = $("#secFooter");
var ht = $(window).height() - $divH.height() - $divN.height() - $divF.height();
$(".divContent").height(ht - 10);
}
});
function CloseBrowser(cmd) {
if (cmd == 'quit') {
open(location, '_self').close();
}
return false;
}
</script>
</asp:Content>
The following image will give more idea what changes I did in both of the above codes.
If you have a problem with some functionality, start simple:
<body>
<script>
window.close();
</script>
</body>
It works just perfect on every browser.
If you would like to close the window that you previosly opened:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_close
From the question itself, I am not able to determine what you really want to achive? Would you like the window to be closed on some event?
I examined your closeBrowser function and rewrote it:
<body>
<script>
var cmd = prompt("Please enter cmd");
if (cmd == 'quit') {
window.close();
}
</script>
</body>
It is also based on 'cmd'. The value of cmd is taken out of the user prompt.
In addition, your first version of code is fine. You just need to invoke the function after creating it:
function CloseBrowser() {
window.open('', '_self', ''); window.close();
}
CloseBrowser();
Now it works on chrome as well. Using this syntax, you can avoid prompt before closing the window. To see the efect, use setTimeout function:
setTimeout(CloseBrowser, 5000);
It will close the window after 5 seconds.
I'm trying to update a javascript variable whenever my vb.net page fires a "doPostBack" event which causes a .net session variable to update.
I tried adding this but it doesn't work.
<script>
var mySessionVariable = '<%= Session("monsterID")%>';
addToPostBack = function (func) {
var old__doPostBack = __doPostBack;
if (typeof __doPostBack != 'function') {
__doPostBack = func;
} else {
__doPostBack = function (t, a) {
if (func(t, a)) old__doPostBack(t, a);
}
}
};
$(document).ready(function () {
addToPostBack(function (t, a) {
mySessionVariable = '<%= Session("monsterID")%>';
});
console.log("mySessionVariable: ", mySessionVariable);
});
</script>
Even though the Session variable changes on the vb.net side after firing an event that does a "doPostBack", it remains the same both before and after the postback on the client side side.
MonsterVar1
MonsterVar1
Is there anyway to get this to work?
Thanks!
Wouldn't make more sense to have a Hidden Field instead of grabbing something from the session to make it work with JS on the page:
<asp:HiddenField id="HiddenField1" runat=Server />
JS
//document.getElementById('<%= HiddenField1.ClientID%>');
var value = document.getElementById("<%= Hiddenfield1.ClientID %>").value
Got a simple test case and not sure why the code is not behaving the same way when calling the url using jQuery's get method.
Code behind:
protected UpdatePanel UpdPanel;
protected PlaceHolder PlHolder;
protected virtual void Page_Load(object sender, EventArgs e)
{
PlHolder.Controls.Clear();
var count = Request.QueryString["count"];
AddControl(string.IsNullOrEmpty(count) ? 10 : Convert.ToInt32(count));
}
public void AddControl(int count)
{
for (var i = 0; i < count; i++)
{
var control = (TestList)Page.LoadControl("~/List/TestList.ascx");
control.TextBoxText = string.Format("{0} - test", i);
PlHolder.Controls.Add(control);
}
UpdPanel.Update();
}
Markup:
<asp:UpdatePanel runat="server" ID="UpdPanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="PlHolder"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Javascript:
<script type="text/javascript">
$(function() {
var link = $('.link'),
url = window.location.href.replace("#", "") + "default.aspx?count=20";
link.on('click', function() {
$.get(url);
});
});
</script>
If I use url http://localhost:54645/default?count=20 directly in browser the UpdatePanel.Update() is working fine, but using the jQuery's get it doesn't do anything.
The page life cycle events are triggered in a same way and there are no differences (at least I think so).
Also tried using $.post() with no change.
Any help appreciated.
you need to put an asp:button INSIDE the panel.
UpdatePanel actually do a full post-back, and the page in the client knows to update the relevant part, so you need to keep this in mind in you Page_Load method.
I want to pass a value from a child page to my parent page:
Parent page script:
<script type="text/javascript">
var popup;
function SelectName() {
popup = window.open("SelDestination.aspx", "Select", "width=500,height=300");
popup.focus();
return false;
}
</script>
On child page:
<asp:Button ID="btnSelect" runat="server" CssClass="button" Text="انتخاب" OnClientClick="SetName();" />
And child page script is:
<script type="text/javascript">
function SetName() {
if (window.opener != null && !window.opener.closed) {
var ddlName = window.opener.document.getElementById("txtDes");
//var e = document.getElementById("<%=ddlOffice.ClientID %>");
ddlName.value = "Hi";
}
window.close();
}
But it doesn't work correctly (I have a runtime error: ddlname is null or undefined)!
Any ideas of how to fix it?
Make a javascript file and do this in head:
<script src="path/to/ye/js"></script>
And remember to do it in both sites.
Then add this variable to to javascript file.
Objective:- From the server-side, I need to open a radwindow(defined in JavaScript of the aspx page) automatically on an IF condition.
Code used:-
In aspx page, I defined the radwindow as:-
<telerik:RadWindowManager Skin="WBDA" ID="AssetPreviewManager" Modal="true"
EnableEmbeddedSkins="false" runat="server" DestroyOnClose="true" Behavior="Close"
style="z-index:8000">
<Windows>
<telerik:RadWindow ID="DisclaimerAlertWindow" runat="server" Width="720px" Height="220px"
Modal="true" visibleStatusbar="false" VisibleTitlebar="false" keepInScreenBounds="true" title="Sourav">
</telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
In JavaScript, a function is defined for opening the radwindow:-
function openRadWindow()
{
var oWnd = radopen('DisclaimerAlert.aspx, 'DisclaimerAlertWindow');
oWnd.set_title('Access Denied !');
oWnd.Center();
return false;
}
So on the server side of the aspx page, In the Page Load event an IF condition is checked and then I'm calling 'openRadWindow()' function as:-
protected void Page_Load(object sender, EventArgs e)
{
if (fieldValue == "False")
{
string xyz = "<script type='text/javascript' lang='Javascript'>openRadWindow();</script>";
ClientScript.RegisterStartupScript(this.GetType(), "Window", xyz);
}
}
Problem:-
But on running this, these JavaScript errors are coming:-
Object doesn't support this property or method.
'undefined' is null or not an object
Please help how to achieve my objective. I am totally stuck.
Hi I want to share with you my solution to create RadWindow dialog in Javascript code only.
We need to implement 2 methods: one for initializing RadWindow dialog, and the last one for recieving the arguments returned after closing the RadWindow. You can do what you want in this second step (e.x postback,...)
Here is my code:
Initializing RadWindow dialog:
function openMyDialog(url, args) {
var manageWindow = GetRadWindowManager();
if (manageWindow) {
var radWindow = manageWindow.open(url, "<your_dialog_name>");
if (radWindow) {
radWindow.set_initialBehaviors(Telerik.Web.UI.WindowBehaviors.None);
radWindow.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);
radWindow.setActive(true);
radWindow.SetModal(true);
radWindow.center();
radWindow.set_visibleStatusbar(false);
radWindow.set_keepInScreenBounds(true);
radWindow.set_minWidth(640);
radWindow.set_minHeight(480);
radWindow.setSize(640, 480);
radWindow.set_destroyOnClose(true);
radWindow.add_close(closeMyDialog);//after closing the RadWindow, closeMyDialog will be called
radWindow.argument = args;//you can pass the value from parent page to RadWindow dialog as this line
}
}
}
Closing the RadWindow dialog:
function closeMoveProjectDialog(sender, args) {
var objArgs = args.get_argument();
//objArgs variable stored the values returned from the RadWindow
//you can use it for your purpose
}
How to call this?
You can put the open method into your expected method. In my side, I have a method as shown below and I will call the RadWindow as this way:
function ShowForeignKeyFrontEditSingle(param1, param2){
var url = "ForeignKeyFrontEditSingle.aspx";
var objArgs = new Array();
objArgs[0] = param1;
objArgs[1] = param2;
openMyDialog(url, objArgs);
return;
}
Of course, you have to declare a RadWindowManager control
function GetRadWindowManager() {
return $find("<%=your_radwindow_manager_control.ClientID%>");
}
Take a look here, it explains how to use the ScriptManager.RegisterStartupScript method: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-javascript-from-server-side.html. Note it the ScriptManager's method. Also look at the Sys.Application.Load event to prevent your code from executing too early.