How to swallow the postback of a asp.net linkbutton control? - asp.net

I have a page with a form containing an element. I have the click event being handled client side by a Jscript function, however, the page is still reloading whenever I click the LinkButton, can this be avoided?
aspx
<body>
<form>
<asp:LinkButton runat="server" ID="thing" OnClientClick="return SomeFunction()" Text="Some Operation" />
</form>
</body>
JScript
function SomeFunction() {
document.getElementById('someText').innerText = 'SomeMessage';
return false;
}

SomeButton.Attributes.Add("onclick", "javascript:SomeFunction(); return false;");
Update: since your updated code is assigned function in ASPX page, you can use this method -
<asp:LinkButton runat="server" ID="thing"
OnClientClick="javascript:SomeFunction(); return false;"
Text="Some Operation" />
OR
<asp:LinkButton runat="server" ID="thing"
OnClientClick="return SomeFunction()" Text="Some Operation" />
function SomeFunction() {
document.getElementById('someText').innerText = 'SomeMessage';
return false; /**** Required *****/
}

To have the return false; working, you will have to add return before the Method Name.
ex. <asp:LinkButtun ID="IDHere" runat="server" OnClientClick="return SomeFunction()" />

Related

PostBack not fireing in window.onbeforeunload with Firefox

I have a main page that contains frames
function attachToTheSaveWhenLeave(sender) {
sender.onbeforeunload = function (ev) {
sender.Save();
};
}
<frameset cols="25%,75%">
<frame id ="Frame1" src="LeftFrame.aspx" />
<frame id ="frmTest" src="FrameContent.aspx" />
</frameset>
In one of the page contained by a frame(FrameContent.aspx), I need to know if the page is discarded in order to fire a PostBack and the server must save the changes made by the user
var isPostBack = false;
window.onload = function (ev) {
window.parent.attachToTheSaveWhenLeave(window);
}
function Save() {
if (!isPostBack)
$("input:submit")[0].click();
}
<form id="formFrame" runat="server" onsubmit="isPostBack=true;">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
In IE the server receives the postback but with Firefox I have to use setTimeOut for this to work
function Save() {
if (!isPostBack)
setTimeout(function () {
$("input:submit")[0].click();
},1);
}
I do not like the solution I have found, and I do not understand the Firefox's behavior.
Can anyone explain to me why is Firefox behaving this way and if there is another way to do this?
Don't have an alternate for you, but Firefox appears to be behaving as documented (https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload). The point of onbeforeunload is just to put up a prompt when navigating away from a page.
Perhaps jquery.unload (http://api.jquery.com/unload/) or window.onunload is what you had in mind.

why ajax loader image always shown using jquery and asp.net

with asp.net code below my ajax_loader image doesn't work well and always shown ..
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblname">
Name</asp:Label><br />
<asp:TextBox runat="server" ID="txbname"></asp:TextBox><br />
<asp:Label ID="lblemail" runat="server">
Email</asp:Label><br />
<asp:TextBox runat="server" ID="txbemail" /><br />
<asp:Label runat="server" ID="lblsugg">
Suggestion</asp:Label><br />
<asp:TextBox runat="server" Rows="3" Columns="20" ID="txbsugg" TextMode="MultiLine"></asp:TextBox>
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_onclick" Text="submit" />
<asp:Label runat="server" ID="lblresultmsg"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<div id="loading">
<p>
<img src="Images/ajax-loader.gif" />
Please Wait</p>
</div>
and jquery code
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxStop(function() {
$(this).hide();
});
any suggestions !?
ajaxStart and ajaxStop works only for ajax request sent by jQuery, if you use other libraries or UpdatePanel, it won't help you.
jQuery only.
Whenever an Ajax request is about to be sent {With jQuery-gdoron}, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.
Example of jQuery ajax request that will fire the ajaxStart and ajaxStop:
$.ajax({
url: 'foo',
...
...
});
You could create a generic way to handle this by adding the following code to a common js include. Here's a quick and dirty example:
Note: Be sure you initialize it by calling SetupGlobalAjaxHandlers on your page load.
function SetupGlobalAjaxHandlers()
{
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
}
function InitializeRequest(sender, args)
{
if(typeof(OnPageInitRequest) != "undefined")
OnPageInitRequest();
}
function EndRequest(sender, args)
{
if(typeof(OnPageEndRequest) != "undefined")
OnPageEndRequest();
}
Then, in any page that includes the js file, you could optionally implement the OnPageInitiRequest() and OnPageEndRequest() methods. There, you could show/hide your loading indicator.
I do recommend, though, that you use an UpdateProgress control as you get the show/hide logic for free.
This technique opens up some possibilities for you, say, if you wanted to disable controls while a partial postback is occurring.

disable button in ascx page prior to postback

How do I disable a button in an ascx page? I know how to do it on an aspx page with:
btnSave.Attributes.Add("onclick",
"this.disabled=true;this.value='Please Wait...';needToConfirm=false;" +
ClientScript.GetPostBackEventReference(btnSave, "").ToString());
But the ClientScript function is not present in the ascx page.
You don't need the ClientScript in code behind, you can also have it directly in the button tag as OnClientClick attribute:
<asp:Button id="btnSave" runat="server" OnClientClick="this.disabled=true; this.value='Please Wait...'; needToConfirm=false;" Text="Save" />
You can access the ClientScript property using Page.ClientScript from your user control (ascx).
you can access ClientScript through the Page property of your control
Page.ClientScript.GetPostBackEventReference(btnSave, "")
Did you try: Page.ClientScript.GetPostBackEventReference?
Here's how I fixed the problem. In every page, I have three divs:
<div align="center" id="divWait" style="display:none"><asp:Label ID="lblSaveorCancel" runat="server"></asp:Label></div>
<div style="display:block" id="divMain">
---- page actual content here ------
</divMain>
<div id="divBut" style="text-align:center;display:block">
<asp:button id="SaveBtn" runat="server" CssClass="button" Text="Save" OnClientClick="return Validate('save');"/>
<asp:button id="CancelBtn" runat="server" CssClass="button" Text="Cancel" OnClientClick="return ShowWaitDiv('cancel');"/>
</div>
And then I added scripts:
function Validate(saveorcancel) {
----- validation checks for data on the page ------
}
function ShowWaitDiv(saveorcancel) {
var div = document.getElementById(divWait.id);
div.style.display = "block";
var div1 = document.getElementById(divMain.id);
div1.style.display = "none";
var div2 = document.getElementById(divBut.id);
div2.style.display = "none";
if (saveorcancel == 'save') {
document.getElementById('<%= lblSaveorCancel.ClientID %>').innerHTML = 'Saving data, please wait...';
}
else {
document.getElementById('<%= lblSaveorCancel.ClientID %>').innerHTML = 'Redirecting, please wait...';
}
return true;
}
Simple, quick, and the user sees the result of clicking a button immediately and cannot click any buttons again.

closing pop up window on button click(if successcede) and reload parent window

I am using this code.This pop up window pops up when I clicks a button on the main page.Now I want the pop up window to be closed if the password is successfully changed and reload the main page,but if the password is not changed then refresh the pop up window again.
I used javascript for form validationenter code here
Here is the code.....
<asp:Textbox id="curnt_paswrd" textmode="Password" runat="server" size="30" />
<asp:Textbox id="new_paswrd" textmode="Password" runat="server" size="30" />
<asp:button ID="btnChange" class="submit-button"
OnClientClick="return validate()" runat="server" Text="Change"
onclick="btnChange_Click" />
You can do simply like this: When you return true the main page automatically refreshes.
function validate() {
var strOutput = window.showModalDialog('ChangePassword.aspx', null, "dialogWidth:750px; dialogHeight:190px; dialogHide:false; edge:raised; center:yes; help:no; scroll:no; status:no;resizable:no;");
if (strOutput == undefined)
return false;
else if (strOutput == '')
return false;
else return true;
}
On your popup page, in javascript:
function IsSavePasswordSuccess()
{
window.returnValue = '<%=hdnSaveClickStatus.Value%>';
window.close();
}
The hidden field will be set as. It will be set in your C# method.
<asp:HiddenField runat="server" ID="hdnSaveClickStatus" />
When you reload the that popup you can do something like this in script if the change was successful:
window.opener.location.reload();
window.close();
So for example using RegisterStartupScript:
ClientScript.RegisterStartupScript(GetType(), "CloseScript",
"window.opener.location.reload(); window.close();", true);

problem with onclientclick and postbackurl

I want to trigger on click of a button this:
<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="country(this.form);" PostBackUrl="http://www.google.com/" />
In javascript I m checking validation of the form.The problem is when I m clicking on the button it is not waiting for the validation but its postback to google.com...
If I do return country(this.form) then on button click it waits for validation but don't postback after I fill the form. I want something like that if javascript validation is false..then OnClientClick should be return country(this.form) if its true then only
country(this.form)
You can do this:
<asp:Button runat="server" ID="submit" Text="Submit"
OnClientClick="if(!country(this.form)) return false;"
PostBackUrl="http://www.google.com/" />
Since postbacks use a "onclick" overall, your script is perpending to the ASP.Net script, returning means none of that postback script runs. If you use an if and only return out if needed, it'll work.
It makes more sense when you look at the rendered result, something like this:
<input type="submit" name="submit" value="Submit" id="submit" onclick="if(!country(this.form)) return false; WebForm_DoPostBackWithOptions(.....)" />
Write it with return:
<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="return country(this.form);" PostBackUrl="http://www.google.com/" />
And your country function will end with return true or false,
function country(form) {
// Validations goes here
return true;// or return false;
}

Resources