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]");
Related
I have created a webview in Android in which i am loading my html file.At the time of clicking a button i am displaying a pop up like alert dialog which is developed in html itself.
Now problem is as soon as i press back button on android phone,application is returning to previous page.But with identical behavior only dialog should be closed and application should be on same page.
How can i achieve it?
Thanks
I think you do not need the JavascriptInterface (as #Henil posted). I would do it like so:
in Java - onBackPressed:
if (webView.getUrl.equals("yourPopupUrl.com")) {
webView.loadUrl("javascript:goAwayPopup();");
return;
}
in JS:
function goAwayPopup() {
var popUp = document.getElementById("popUp"); // in my case popUp is just a <div>
var isVisible = popUp .offsetWidth > 0 || popUp .offsetHeight > 0;
if (isVisible) {
popUp.style.display = 'none';
}
}
EDIT:
Solution with JavascriptInterface:
in JS - function which is called when dialog popups:
function popUp() {
if (typeof Android != "undefined"){
if (Android.popUp!= "undefined") {
Android.popUp();
}
}
}
in Java:
define a inner class and a boolean field in your Activity
declare the bridge to your webview
override onBackPressed
private boolean isPopUpVisible = false;
// some code...
webView.addJavascriptInterface(new MyJavascriptBridge(), "Android");
// some code...
class MyJavascriptBridge {
public void popUp() {
if (isPopUpVisible)
isPopUpVisible = false;
else
isPopUpVisible = true;
}
}
onBackPressed:
if (isPopUpVisible) {
webView.loadUrl("javascript:goAwayPopup();");
return;
}
JS, again:
function goAwayPopup() {
// close alert here...
popUp(); // sets isPopUpVisible to false
}
Here is a post of closing a alert: How to "auto close" Alert boxes
Use java script interface and handle html page from that interface
I am creating a safari extension. When the user right-clicks on a link in safari, it should bring up the context menu. When the user clicks on "Get URL", it should open the clicked on url in a new window. I can't figure out how to get the url! It always opens "not found" instead.
injected.js :
document.addEventListener('contextmenu', handleContextMenu, false);
function handleContextMenu(event)
{
var target = event.target;
while(target != null && target.nodeType == Node.ELEMENT_NODE && target.nodeName.toLowerCase() != "a")
{
target = target.parentNode;
}
if(target.href)
{
safari.self.tab.setContextMenuEventUserInfo(event, target.href);
}
else
{
var foo = "href not found";
safari.self.tab.setContextMenuEventUserInfo(event, foo);
}
}
Global.html:
<!DOCTYPE HTML>
<script>
var lastUrl;
safari.application.addEventListener("contextmenu",handleContextMenu,false);
safari.application.addEventListener('command', handleCommand, false);
function handleContextMenu(event)
{
var query = event.userInfo;
lastUrl = query;
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
function handleCommand(event)
{
if(event.command === 'getUrl')
{
if (lastUrl)
{
safari.application.openBrowserWindow().activeTab.url = lastUrl;
}
else
{
safari.application.openBrowserWindow().activeTab.url = "not found";
}
}
}
</script>
How do I get the url? It always opens "not found" instead.
Why not just have var last url = event.userInfo in the handleCommand function? The userInfo should be defined at that point, and it should be more predictable that trying to set the value on the contextmenu event.
I don't understand why your code is not working, but there are a couple of things you might want to change anyway.
First, in the injected content script, if there's no target.href, don't bother calling safari.self.tab.setContextMenuEventUserInfo.
Second, in the global script, change your handleContextMenu function as follows:
function handleContextMenu(event) {
if (event.userInfo) {
event.contextMenu.appendContextMenuItem("getUrl", "Get URL");
}
}
That way, if the user didn't right-click a link, the context menu item won't be inserted.
Third, as Matt said, you don't need the lastUrl global variable, unless it serves some other purpose. You can refer to event.userInfo directly in handleCommand. And you don't need to check whether it's empty, because the context menu will only be inserted by handleContextMenu if it's not.
function handleCommand(event) {
if (event.command === 'getUrl') {
safari.application.openBrowserWindow().activeTab.url = event.userInfo;
}
}
Hope this helps.
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.
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.
I have multiple update panels with various asp buttons on a single page. I want to disable the buttons which caused the postback in update panel untill it completes.
Is there a way to avoid using a third party control for this? through JQuery or any other method ?
You can either do this:
cs
//in pageload
//the request is not in postback or async mode
bt1.OnClientClick = "this.disabled = true; " + ClientScript.GetPostBackEventReference(bt1, null) + ";");
Note: you can replace "this.disabled = true" with a js function that will have better handling for disabling the button and maybe display a friendly message as well.
Or this:
http://msdn.microsoft.com/en-us/library/bb383989.aspx
js
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus);
function CheckStatus(sender, arg)
{
var postBackElement = arg.get_postBackElement();
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack() && postBackElement.id == "btn1") {
arg.set_cancel(true);
//display friendly message, etc
}
}
Note: I modified it so it checks for the button's id. Replace "btn1"
Good luck!!
You can use the start and stop message of the update panel to disable your controls.
For example
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
document.getElementById("ButtonToDisable").disabled = true;
}
function EndRequest(sender, args) {
document.getElementById("ButtonToDisable").disabled = false;
}
</script>