Disable Submit button in case of multiple click.. (C#) - c#

My Problem is ,I have a simple web form, which contains two textboxes and a button.there are some asp.net validator controls on page.so i want client side disabling of button when all validation is done.and also after disabling of button, i am executing some server side code.All of this is working fine but, in case when I set postback url of button it gets fail. bellow is some part of coding that will give you some brief idea. Any hint will be highly appreciated.......
I wanted to make this functionality in composite control.
here is button class
public class MyButton : Button
{
protected override void OnPreRender(EventArgs e)
{
if (this.CausesValidation)
{
if (!String.IsNullOrEmpty(this.ValidationGroup))
{
this.Attributes.Add("onclick", #"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate('" + this.ValidationGroup + "')){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
else
{
this.Attributes.Add("onclick", #"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate()){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
}
else
this.Attributes.Add("onclick", "javascript:" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this));
base.OnPreRender(e);
}

This is the correct and simple way to do this:
Create a helper method in your application (say in a Utlity Namespace):
Public Shared Sub PreventMultipleClicks(ByRef button As System.Web.UI.WebControls.Button)
button.Attributes.Add("onclick", "this.disabled=true;" & button.Page.ClientScript.GetPostBackEventReference(button, String.Empty).ToString)
End Sub
Now from the code behind of each of your web pages you can simply call:
Utility.PreventMultipleClicks(button1)
where button1 is the the button you want to prevent multiple clicks.
What this does is simply sets the on click handler to: this.disabled=true
and then appends the buttons own post back handler, so we get:
onclick="this.disabled=true";__doPostBack('ID$ID','');"
This does not break the default behaviour of the page and works in all browsers as expected.
Enjoy!

If you disable the button then form submission will not happen. Correct way would be to set timer to disable the button. I would also suggest to use submit behavior instead of putting post-back event ref. For example,
function clickHandler(id, validate, validationGroup) {
var isValid = true;
if (validate && typeof(Page_ClientValidate) == 'function') {
isValid = validationGroup? Page_ClientValidate(validationGroup): Page_ClientValidate();
}
if (isValid)
{
// set timer to disable the button
var b = document.getElementById(id);
var f = function() { b.disabled = 'disabled'; };
setTimeout(f, 100);
return true;
}
return false;
}
And now attach function to your button
protected override void OnPreRender(EventArgs e)
{
this.Attributes.Add("onclick",
string.Format("return clickHandler('{0}', {1}, '{2}')",
this.ClientID, this.CausesValidation ? "true" : "false",
this.ValidationGroup));
}

If you disable a submit button once it is clicked, then it won't postback. I've been researching this for many many hours now, and the best solution I've seen is here. (The best solution is at the bottom of the page)
I'm now writing a custom server control to add to the toolbox that extends from Button, and uses a slightly modified version of this code. (overriding the OnLoad method)
I'm allowing the user the ability to change the 'processing...' text for something else and may make an attribute that allows the text to change to something else when submission is complete(which would be produced when the postback returns)

I have come up with the solution. Just hide the button after you click it. The postback will take place as usual. After completion of the postback, you will get your button as it is! See the detailed steps on how to disable asp.net button on postback and master the art!

Assuming this is asynchronous, and that you validate prior to saving, you can try as pseudo-follows:
bool hasSavedAlready = false;
savedata(){
if (!hasSavedAlready){
//normal saving code
...
//after success
hasSavedAlready = true;
}
}
That's the simplest solution I can think of.

I had this problem and none of the previous solutions worked for me but after some messing around I used:
<asp:LinkButton runat="server" OnClientClick="this.setAttribute('disabled','disabled'); this.text = 'Submit in progress...';" UseSubmitBehavior="false" ID="btnSubmit" ValidationGroup="formSubmit"</asp:LinkButton>

You need to consider that if the same session/user opens a page (example.whatever) on a window or tab lets say window1 ,and he again opens the same page (example.whatever) on window2.
When the user clicks the button on window1 and before 30 secs ends he clicks that button on window2 the server will think you clicked that button twice.
To avoid this in asp.net, i use :
Application["isButtonGettingClickedByAnyOne"] this an array that is shared bettwen all Sessions (all the users of your website).
or
Session["isButtonGettingClickedByThisUserInAnotherTab"] you can see it as shared bettwen all tabs (of the same user).
And make the server checks if they are reserved by some one or not.
if yes then wait 30sec + you turn ...
if no then you can 1st reserve it, and then execute your function, and when you finish the function free it so that others can use it again.
I hop this was useful and not painful to read.

Related

C# use a method on another form

On my main form in my Windows C# application I have this method:
public void uploadNew(String newName, String filePath)
{
//Make sure file doesn't exist, then continue
if (!File.Exists(basePath + POnumber + newName))
{
File.Copy(filePath, basePath + POnumber + newName);
LogSubmit("Added New File " + newName);
listFiles();
}
else
{
//The file already exists
}
}
The meaning of the code really doesn't matter. Here's what I am trying to do. I needed a new form for a file upload form with some options and such. What I need is to send two variables back to the main form to plug into that method.
The operation is essentially this.
Main form -> Click button -> Form 2 -> operations -> Send back variables to uploadNew -> Close Form 2
I think what you want is a dialog. Essentially, you can do something like this in the MainForm:
using(var form = new Form2())
{
if(form.ShowDialog() == DialogResult.Ok)
{
// Newname and Filepapth are properties you set in the Form2
uploadNew(form.NewName, form.Filepath);
}
}
In the Form2, you probably will have a Ok, Cancel Button. For the Ok Button event, you can set this.DialogResult = DialogResult.Ok. On Cancel event, you can set it to something else.
This is from the top of my head, so some things might be off, but the concept is the same.
Also, a good idea is to move UploadFile to a new class, so you do not tie your business logic into your UI logic.
A more complete example here: How to return a value from a Form in C#?

Using Window.Open instead of Response.Redirect to open new window?

My page code looks like this:
<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click"/>
My method looks like this:
protected void btnSearch_Click(object sender, EventArgs e)
{
var value = lblGraphicNameValue.Text.ToString();
Response.Redirect("Search.aspx?txtGraphicName=" +
value);
}
Currently, when the user press the 'Search' button the page refreshes and loads the Search.aspx page. What I'd like to happen is have the Search.aspx open in a new window, instead. I've looked at using Window.Open, but I'm not sure if this is the correct route, or if I can use the same method of passing in my variable (querystring). Can someone point me in the right direction? What I have works, I just want it to open in a new page while leaving the prior page alone.
EDIT: I should mention that I cannot use javascript (secure environment, every browser has javascript disabled).
From what I'm reading, it seems to indicate that opening a new web page from within an asp.net page and having parms passed in is not do-able without javascript? Is this correct?
This code below ultimately does exactly what I needed it to:
<a href="<%= this.ResolveUrl("Search.aspx?id=" + lblGraphicNameValue.Text.Remove(lblGraphicNameValue.Text.Length -4)) %>"
target="_blank">Search Related</a>
This code does three things:
1) Opens Search in new page.
2) Truncates the search value by four
characters (I only needed part of the search string)
3) Passes in
parameter to new page.
This accomplished exactly what I needed without resorting to custom classes or javascript, although it did make me have to use a link instead of a button.
Use this class.
ResponseHelper .Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");
public static class ResponseHelper {
public static void Redirect(string url, string target, string windowFeatures) {
HttpContext context = HttpContext.Current;
if ((String.IsNullOrEmpty(target) ||
target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
String.IsNullOrEmpty(windowFeatures)) {
context.Response.Redirect(url);
}
else {
Page page = (Page)context.Handler;
if (page == null) {
throw new InvalidOperationException(
"Cannot redirect to new window outside Page context.");
}
url = page.ResolveClientUrl(url);
string script;
if (!String.IsNullOrEmpty(windowFeatures)) {
script = #"window.open(""{0}"", ""{1}"", ""{2}"");";
}
else {
script = #"window.open(""{0}"", ""{1}"");";
}
script = String.Format(script, url, target, windowFeatures);
ScriptManager.RegisterStartupScript(page,
typeof(Page),
"Redirect",
script,
true);
}
}
}
I think your on the right track, but you're confusing server side code, and client side code. window.open is a Javascript function which works on the client side. So you'll need to render some Javascript from C# to make the window popup. Try:
protected void btnSearch_Click(object sender, EventArgs e)
{
var value = lblGraphicNameValue.Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('Search.aspx?txtGraphicName={0}');</script>", value));
}
That will re-render the page, and then add a script on pageload that will popup the window. A little warning, this will probably be blocked by a browser popup blocker. If you want to get around that, you can probably achieve this without posting back to the server by using Javascript.
A better option would be to create a javascript function like:
function PreviewPOSTransaction(Id)
{
if (Id != null)
{
window.open('POSTransReport.aspx?TransID=' + Id);
return true;
}
}
</script>
and call this function on button "OnClientClick" event like:
OnClientClick="PreviewPOSTransaction(1);

calling web method on window onbeforeunload event

I am working on basic ASP.net website and i want to execute server side function when user try to go away from page. For this i am using onbeforeunload event of window. I have check box on my page and when user checked this check box, i am executing sverside "checkedchange event". Issue is whenever user click on this check box my web method is also get called, which should not get called as only postback is happen, user is not leaving my page. can any one suggest me to avoid web method call when postback happen.
I wnat to execute web method only in following scenarios:
1) When user closes the browser.
2) On click of “Find more matches” button, when user landed on search results page with no school listed.
3) when user changes the url from browser's address bar
Code on aspx page:
function GetMessage() {
var urlstring = document.URL;
{
PageMethods.Message( document.URL);
}
}
</script>
Code on aspx.cs page
[System.Web.Services.WebMethod]
public static void Message()
{
string x="a";
}
This link should hold the answer you are looking for: How to capture the browser window close event?
I think the following code from that link is what you are looking for.
var inFormOrLink;
$('a').live('click', function() { inFormOrLink = true; });
$('form').bind('submit', function() { inFormOrLink = true; });
$(window).bind('beforeunload', function(eventObject) {
var returnValue = undefined;
if (! inFormOrLink) {
//TODO: Execute some code before unload here
//returnValue = "Message to display before the user leaves the page.";
}
eventObject.returnValue = returnValue;
return returnValue;
});

Identify during Page.Load if Button click event is going to be handled

I have ASPX web page which has a Button on it. Once user click this button, request is submitted to server and button click event handler is executed.
I have some logic that must reside on Page.Load, but this logic depends if request has been submitted by button click. Based on page life cycle event handlers executes after Page Load.
Question: How in Page load I can find out what event handlers are going to be executed after Page Load?
#akton's answer is probably what you SHOULD do, but in case you want to go off the reservation and determine what is causing a postback early on in the lifecycle, you can interrogate the postback data to determine what was clicked. This will NOT give you what actual functions/handlers will be executed during event handling, however.
First, if something other than a Button/ImageButton caused the postback, the ID of the control will be in __EVENTTARGET. If a Button caused the postback, there is something "cute" ASP.NET does: it ignores all other buttons so that only the clicked button shows up on the form. An ImageButton is a little different, because it will send coordinates. A utility function you can include:
public static Control GetPostBackControl(Page page)
{
Control postbackControlInstance = null;
string postbackControlName = page.Request.Params.Get("__EVENTTARGET");
if (postbackControlName != null && postbackControlName != string.Empty)
{
postbackControlInstance = page.FindControl(postbackControlName);
}
else
{
// handle the Button control postbacks
for (int i = 0; i < page.Request.Form.Keys.Count; i++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
if (postbackControlInstance is System.Web.UI.WebControls.Button)
{
return postbackControlInstance;
}
}
}
// handle the ImageButton postbacks
if (postbackControlInstance == null)
{
for (int i = 0; i < page.Request.Form.Count; i++)
{
if ( (page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length-2) );
return postbackControlInstance;
}
}
}
return postbackControlInstance;
}
All that being said, if you can refactor your control/page to delay execution, your code will be much cleaner/more robust if you use the paradigm suggested by #akton.
There may be a better solution to the problem. Do you want the code to only run when the page is first loaded and you are using postbacks? If so check the Page.IsPostBack property. If the code does not need to run before other event handlers, move it to OnPreRender because it fires after event handlers.
These helped me a lot: I wanted to save values from my gridview, and it was reloading my gridview /overriding my new values, as i have IsPostBack inside my PageLoad.
if (HttpContext.Current.Request["MYCLICKEDBUTTONID"] == null)
{ //Do not reload the gridview.
}
else { reload my gridview. }
SOURCE: http://bytes.com/topic/asp-net/answers/312809-please-help-how-identify-button-clicked

ShowModalDialog of javascript is not closing from ASP Code behind and opening a new pop up of same page

i m opening an Webpage(Clock.aspx) from window.showModalDialog of javascript.In the Clock.aspx i have a button and i want that when the user click on that button the Clock.aspx page will be closed. I dont want to use onClientClick() method of javascript as some server side database insertion is going on and after the insertion i want to close this page.
The Code behind of the button is as follows:-
protected void btnStop_Click(object sender, EventArgs e)
{
_nonProduction = new NonProduction();
if (Session["LastNonProdTimeID"] == null)
{
}
else
{
int NonProdTimeEntryID = Convert.ToInt32(Session["LastNonProdTimeID"]);
//Updating the TimeSpent
isTimeSpentUpdated = _nonProduction.UpdateTimeSpentInDB(NonProdTimeEntryID);
if (isTimeSpentUpdated == true)
{
string timespent = Convert.ToString(_nonProduction.GetTimeSpent(NonProdTimeEntryID));
string msg = "Total time consumed in " +HiddenTaskname.Value.ToString()+": " + timespent.ToString() + " Minutes";
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>");
//ShowPopUpMsg(msg);
}
else
{
}
}
}
Here when i m clicking on the Button one more (Clock.aspx)pop up is appearing and window is not closing. Please help me that how i close the ShowModalDialog from server side code. I m using Script manager in my page also.
Thanks in Advance.
I had added <base target="_self">to the head section of the clock.aspx page and then it works fine for me.
For mine, I have a function in normal javascript that closes the page in the aspx.
In the code behind, if the update is successful, it calls that function.
// this function is to be called by the popup windows to refresh the opener using specific office code, and close self
function allDoneOffice(office)
{
var opener = self.opener;
if (opener.doRefresh) opener.doRefreshWithOfficeCode(office);
window.open('','_self',''); // IE warning hack
self.close();
}
// update the record
bool b = report.SaveModifiedToDB();
if (b)
{
// don't close the page if nothing was updated
ClientScript.RegisterStartupScript(this.GetType(), "load", "<script type=\"text/javascript\">\n" +
"allDoneOffice('" + report.OfficeCode + "');" + "<" + "/script>");
}
else
{
lblResults.Text += " Unable to save modified report to the database.";
}
Use this code below should work in IE
Response.Write("<script language='javascript'> { self.close() }</script>");

Categories

Resources