Executing client-side script on link click - c#

I need my web page to open a window and enable a disabled button when a link or a button is clicked.
From what I've read on other posts on here, if I try and open a new window in Page_load most browsers will assume it's a pop-up and block it so I've been trying to do it client side with JS.
Currently, I'm trying it with a link declared like so:
Please click here to open the document.
This calls the following JS:
function OpenDoc()
{
<%= btnSubmit.ClientID %>.Visible = true;
Window.Open('GetDocument.aspx')
}
Unfortunately, instead of rending the JS as "btnSubmit.Visible = true" it comes out as "MainContent_btnSubmit.Visible = true" which doesn't work.
Assuming this is the best way of doing what I want, where am I going wrong?

You can't change visibility property through javascript but you can use the following code instead of it :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.disabled = true;
In this case the button will be disabled and if you need to hide button not disableing it then use the following code :
var control = document.getElementById('<%=btnSubmit.ClientID %>');
control.style.display= "none";
Hope this is helpfull based on my understanding to your problem

Related

Save current window URL and move back to that URL after couple of navigation

I have a site and in my site when user click on a specific div then a new child window open . Suppose the user is standing at xyz.com/category , now if you user clicks on that div then parent windows goes to another url suppose google.com and a popup window will get open and there are some more navigation in popup windows . But every page that will open in popup has a button called Go Back To Site . I want that when user clicks on several links in popup and then click on Go Back To Site button then popup window get closed and parent window move back to xyz.com/category.
You can try PHP Superglobal array
$_SERVER['HTTP_REFERER']
If you can use javascript for this, there are simple functions such as:
window.history.back();
window.history.forward();
You can refer all of them in following link:
https://developer.mozilla.org/en/docs/DOM/Manipulating_the_browser_history
For Jquery:
$(document).ready(function() {
var referrer = document.referrer;
});
For C# :
WebBrowser1.Url = new Uri("http://maps.google.com");
- document.referrer does not work for IE
For this issue refer following code:
<script type="text/javascript" >
function redirect(url) {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
var referLink = document.createElement('a');
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
} else {
location.href = url;
}
}
</script>
This code can also be refered over here
The way I'd do it is to pass the current URL as a parameter into your popup. So you would change your anchor link tags to this:
Click
So when your popup pops up, you just grab the value of prevPage and then when they click "Back to previous page", you just redirect to the value of prevPage.

Conditional popup on html document close

I have a content management page on my CMS on which I have a popup that warns the user on page close. It is a very simple warning implemented like so:
window.onbeforeunload = function (e) {
return 'Are you sure you want to close?';
}
It works, but the problem is that the popup is shown even when the user clicks the same button. I am very new to document events so I have no idea how to do this, but I want the popup to only show when the page is being closed and not when it is being posted back.
Also I have not used JQuery in my website so please don't suggest it; I don't want to use a whole framework just for a popup.
.
Thanks.
I was able to get it to work by using a global variable, like so:
var check = true;
Which is checked for when showing the popup:
window.onbeforeunload = function (e) {
if (check) {
return 'Are you sure you want to close?';
}
}
Then you simply set check to false when you click a button that does a postback. Hope this helps someone.

How to make the 'Are you sure you want to navigate away from this page' warning message in browser?

I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?
I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?
This is one of the multiple ways to achieve the same thing
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
got it from here open js
Only the unload() event will work on JS. You can't manage it on the server.
Check out the answer to this other question on SO, it is very similar to your question
How to show the "Are you sure you want to navigate away from this page?" when changes committed?
Simple solution
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Are you sure you want to leave this page?";
}
4guysFromRolla.com - Prompting a user to Save when Leaving a Page
You cannot use the onbeforeunload window method as it gets triggered by multiple ways like back and forth browser navigation links, refreshing the page, closing of the page, clicking on the links.
What i feel you have to bind the link tag for which you want display the navigation away message and then use the function for the status message display
window.addEvent('domready',function(){
$$('a').addEvent('click', function(e) {
//leaving(); function u wrote for displaying message
});
});
function leaving(e) {
if(!e)
e = window.event;
// return code for the displaying message
}
If you want to do this in a way that guarantees it will work on almost all browsers, use the JQuery library. The following describes the unload event.
http://www.w3schools.com/jquery/event_unload.asp
It's exactly for purposes like yours.
Just to elaborate a little, you would have to download the jquery js library and reference it in your project/page, but you'll probably want to do that eventually anyway.
If you want to control this from the server side, you can dynamically emit the jquery call in the OnPreRender.
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.
I was able to get this to work with Andrei G's answer. I would add on that to get it to work in Chrome, add this to the end of his goodbye function:
return "";

Page doesn't postback after clicking OK in confirm box for Radiobuttonlist

I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.

how disable / enable An Ajaxifeid Button From Client Side (JavaScript)

i have an AJAXIFIED button(btnsend) thas is disable by it's Property -> Enabled="False"
i have a TextBox Next To This Button And I Want Enable that Button When Users Type Something in That TextBox...
so i did this (JavaScript):
function onkeyupontextbox() {
var btnSend = document.getElementById("btnSend");
btnSend.disabled = false;
}
but that button does not work after becomes enable...
what can i do about that?
(i am using radajaxmanager for ajaxify that button)
(when i remove that button from RadAjaxmanager Or UpdatePanel So EveryThing Is Ok , But I Want That Button In Ajaxify Mode)
thanks for your attention...
Sounds like you're trying to mix Ajaxified properties and DOM element properties.
Leave the property Enabled = "True" when you ajaxify it, then use JS on page load to btnSend.disabled = true it. If you use pure js to disable it the function you have above should work fine to re-enable it. For example, if the ajaxify property 'Enabled' is set to true, then place the following javascript into your page:
window.onload = function(){
document.getElementById("btnSend").disabled = true;
};
Then use the function you wrote above to enable it onkeyupontextbox(). Because javascript is disabling the button, it should be able to re-enable it. Before, you were disabling with the Ajaxified property and trying to re-enable with js.
Could following be the answer? (Dont have experiences with RadAjaxmanager)
function EnableBtnSend()
{
$find("<%=btnSend.ClientID %>").ajaxRequest("");
}
Found here: http://www.telerik.com/help/aspnet-ajax/grdenabledconventions.html

Categories

Resources