Dynamically open a radwindow defined in Javascript - javascript

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.

Related

Unable to copy the text in clip board inside the repeater

I am trying to copy the text in clip board inside the repeater but it's not copying.
Below is what I have tried so far.
protected void rptCopy_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) {
if (e.CommandName == "Copy") {
System.Web.UI.WebControls.LinkButton btnCopy = (LinkButton) rptQuestResponseDtl.Items[0].FindControl("lnkCopy");
System.Web.UI.WebControls.Label txtMsg = (Label) rptQuestResponseDtl.Items[0].FindControl("lblComment");
txtMsg.Focus();
btnCopy.Attributes.Add("onclick", "function copyClipboard(){ CopiedTxt = document.selection.createRange();CopiedTxt.execCommand('Copy'); }");
}
}
should the onclick event be onclientclick like btnCopy.Attributes.Add("onclientclick",... ? Also you defined the function copyClipboard but never called it like copyClipboard()... My recommendation is define function in JS file, include it in your ASPX page and then use clientclick event to call the function

Dynamically added button omits confirm

I'm developing a website for users where I add controls dynamically.
The problem is that after a confirmBox appears it doesn't matter what I click (Ok/Cancel) it still deletes my objects.
This is how I add them from codeBehind:
aPanel.RegisterAction("DeleteStuff", "Delete object",
true, btnDeleteClick, null);
where aPanel is ActionPanelDx
right after this comes:
if (actionPanel["DeleteStuff"] != null)
actionPanel["DeleteStuff"].ClientSideEvents.ItemClick =
"function(s,e){return confirm('Are you sure you want to delete?')}";
protected void btnDelete_Click(object sender, MenuItemEventArgs e)
{
//Im using self written classes for handlig SQL logic it looks like this:
MySQLCommand commad = new MySQLCommand("delete_object");//procedure
commad.MyParam.AddWithValue("#ob_id", ObjectID);
commad.myExecuteNonQuery();
}
Am I using the JS function in a wrong?
Now what your code does it to delete your object whenever a button (doesn't matter which) is clicked. What you need to do is something like that:
protected void btnDelete_Click(object sender, MenuItemEventArgs e)
{
if (e.item.name === "Yes")
{
MySQLCommand commad = new MySQLCommand("delete_object");//procedure
commad.MyParam.AddWithValue("#ob_id", ObjectID);
commad.myExecuteNonQuery();
}
}
instead of e.item.name it could be e.item.text or something like that, put a breakpoint or console.log to see what is inside of your e property if you not sure.

Click on control cause unwanted JS call

I have an unexpected behavior of two JS which get called when I click on a control.
These JS are supposed to be called only when the button in the Tree list is clicked under specific conditions.
Right now the JS "message alert" is called even if a click on any of the node of the tree list when the conditions apply.
The other JS, which open a window, also opens when a node of the tree list is clicked, but after having opened and closed it at least one time.
protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
{
string idMessage = "";
if (e.CommandName == "Select")
{
if (e.Item is TreeListDataItem)
{
TreeListDataItem item = e.Item as TreeListDataItem;
idMessage = item.GetDataKeyValue("MessageID").ToString();
}
}
addMessage(idMessage);
}
private void addMessage(string idMessage)
{
if (Label1.Text =="" || Label1.Text==null )
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('You shall be logged-in to post and replay to messages');", true);
}
else
{
{
Session["fatherMessageID"] = idMessage;
string script = "<script language='javascript' type='text/javascript'>Sys.Application.add_load(ShowWindow);</script>";
ClientScript.RegisterStartupScript(this.GetType(), "showWindow", script);
}
}
}
Function which opens the window:
function ShowWindow() {
var oWnd = window.radopen('Window1.aspx', 'window1');
}
Function which close the window from inside the window:
function GetRadWindow() {
var oWnd = null;
if (window.radWindow) oWnd = window.radWindow;
else if (window.frameElement.radWindow) oWnd = window.frameElement.radWindow;
return oWnd;
}
function CloseWindow() {
var oWnd = GetRadWindow();
oWnd.close()
}
Function which calls the CloseWindow inside the window page:
finally
{
string script = "<script language='javascript' type='text/javascript'>Sys.Application.add_load(CloseWindow);</script>";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", script);
}
How can I fix this issue?
Alert Issue:
you need to place addMessage(idMessage); inside if (e.Item is TreeListDataItem) condition
Dialog Issue:
not sure whether window.radopen('Window1.aspx', 'window1') is correct or not. if you are using RadWindow then showwindow function should be something like this var oWnd = window.radopen(null, "[RadWindowID]");

window.parent.location.href not working with ajaxcontroltoolkit in asp.net

I am using iframe to open new .aspx page from parent page. child page is using ajaxcontroltoolkit(ajax CalendarExtender). Now on form submit, I want to close iframe and return to parent page. For that I am using following code.
ClientScript.RegisterStartupScript(this.GetType(), "scriptid", window.parent.location.href='ViewVendors.aspx'", true);
This works file if I remove ajax control from child page but does not work with ajax control.
I want to use calenderExtender and iframe both. How can I use it and what is the problem for such so called abnormal behavior.
This is the code for my submit button event handler.
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
objVendor.VendorID = Convert.ToInt64(Request.QueryString["Id"]);
objVendor.Name = txtName.Text;
objVendor.BillingAddress = txtBillingAddress.Text;
objVendor.ShippingAddress = txtShippingAddress.Text;
objVendor.ContactPersonName = txtContactPerson.Text;
objVendor.ContactNumber = txtContactNumber.Text;
objVendor.EmailID = txtEmailID.Text;
objVendor.VendorSinceDate = Convert.ToDateTime(txtVendorDate.Text);
objVendor.IsActive = Convert.ToBoolean(rdblStatus.SelectedValue);
objVendor.Logo = FileUpload();
int intResult = objVendor.UpdateVendor();
if (intResult > 0)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "window.parent.location.href='ViewVendors.aspx'", "scriptid", true);
//ClientScript.RegisterStartupScript(this.GetType(), "scriptid", "window.parent.location.href='ViewVendors.aspx'", true);
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.CssClass = "ERROR";
}
}
//Edit
Now my code works fine as long as I am not adding calender extender to the child page.
When I add calender extender in child page it shows error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)". If I remove calender extender, again it works well. By doing some googling, I found that <% %> in Javascript tag is creating problem. How can I solve it and why calender control is creating problem in such cases?
Here is the code for my script.
<script type="text/javascript">
function uploadStarted() {
$get("imgDisplay").style.display = "none";
}
function uploadComplete(sender, args) {
var imgDisplay = $get("imgDisplay");
// var imgPhoto = $get("#imgPhoto");
var imgPhoto = document.getElementById('<%=imgPhoto.ClientID %>');
imgDisplay.src = "images/loader.gif";
imgPhoto.style.display = "none";
imgDisplay.style.cssText = "";
var img = new Image();
img.onload = function () {
imgDisplay.style.cssText = "height:100px;width:100px";
imgDisplay.src = img.src;
};
img.src = "<%=ResolveUrl(UploadFolderPath) %>" + args.get_fileName();
}
</script>
You need to register your JavaScript using the ScriptManager instance on your page - which you should already have if you're using AJAX. It has its own RegisterStartupScript method that you can use.

Closing RadWindow using RadButton

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.

Categories