How can I prevent double submit of link button. I have a payment button, I want to disable it as soon as somebody clicks on it and re-enable it if the payment fails. I am using AJAX and programmatic post to payment gateway.
I can disable the linkbutton using this code:
asp:LinkButton ID="btnPayment" runat="server" onClientClick="this.disabled=true;__doPostBack(this.id,'');" Text='<%#Databinder.Eval(Container.DataItem,"Author")%>' ></asp:LinkButton>
But how do I enable it back when payment fails?
I think in the button click handler if the payment failed you can write the code to execute javascript like:
ScriptManager.RegisterStartUpScript(this.Page, typeof(Page) , "enableBtn" , "$('[$=btnPayment].removeAttr(disabled)')" , true);
This requires jquery.
ALternatively you can call document,getelementbyID also if you dont want to use jquery.
Let me know if this is not what you want
I have a one more solution but that seems to be nasty:
In the beginning
<asp:LinkButton ID="btnPayment" runat="server" Text='<%#Databinder.Eval(Container.DataItem,"Author")%>' ></asp:LinkButton>
And using jquery do this:
$(document).ready(function() { $('id$=btnPayment').live("click", function(e) {
$(this).attr("disabled", "disabled");
__doPostBack($(this).attr("id"),'');
e.preventDefault();
}); });
So with Ajax you removed the disabled attribute so the link button becomes clickable again.
Will this make sense
In code behind, You can enable and disable code.
On click, first disable link button and enable it if payment succeed.
because above code will also do server trip, ajax call is not firing.
btnLogin.Enabled = false;
if (txtUserName.Text == "hh") btnLogin.Enabled = true;
Related
I'm new to web programming with .NET.
I am developing a web page with webforms, and I want at a certain moment to programmatically show a modal window, for the user to accept or cancel, according to a question. Exactly what does the "confirm" function of JavaScript.
I tried to get it calling a JavaScript function:
Page.ClientScript.RegisterStartupScript (this.GetType (), "CallMyFunction", "MyFunction()", true);
But I need to do it without reloading the page, and I also need to control if the user has accepted or canceled and I do not know how to do it.
I've also tried getting it using the ModExPopupExtender control from DevExpress.
Can someone tell me a simple way to get what I want?
I can not understand how something so usual in web programming, and that PHP + javascript would not pose any problem can be so complicated.
All start in a one-button event on the code behind:
protected void btn1_Click(object sender, EventArgs e)
{
//I make a series of checks
//If certain conditions I want to show the confirm
//According to the user has chosen ok or cancel will perform a certain action
}
Onclientclick does not help me because before launching the "confirm" I have to do some checks on the server side.
Thank you very much.
You can use OnClientClick which is a property on most web controls.
I like to just bring up a simple confirm() dialog which executes the server code if the user clicks OK and does nothing if the user cancels the action:
<asp:Button runat="server" ID="btnSave" Click="btnSave_Click" Text="Save"
OnClientClick="return confirm('Are you sure you want to do this thing?');" />
You can do other things with it as well, but the key thing to remember is that anything you do in OnClientClick will happen before the page gets posted back to the server.
This is also perfectly valid:
<asp:Button runat="server" ID="btnSave"
OnClientClick="showModalConfirm('some message goes here');" ... />
<script>
function showModalConfirm(msg)
{
$(".modal .message").innerHtml(msg);
$(".modal").Show();
}
</script>
You can set the action that OnClientClick should perform in your codebehind in exactly the same way:
protected void Page_Load(object sender, EventArgs e)
{
btnSave.OnClientClick = "return confirm('Are you sure you want to do this thing?');";
}
You can use below code in c# to call javascript function. Below code will execute afterpostback() javascript function:
ClientScript.RegisterStartupScript(GetType(), Javascript, "javascript:afterpostback();", true);
And you can write code in javascript function to display any div or popup:
<script language="javascript" type="text/javascript">
function afterpostback() {
//Here you can write javascript to display div/modal
}
</script>
One way I've handled this previously was to have 2 buttons on the page. The first would be initially visible and labeled "Submit". The second would be initially hidden and labeled "Confirm". The "Submit" button would postback upon click and perform your server side checks/validation. If those checks failed, an appropriate error message would be displayed. If those checks passed, an appropriate "Please confirm your submission"-type message would be displayed, the "Submit" button would become hidden, and the second "Confirm" button would become visible. When that Confirm button was clicked, it would postback again and fully submit.
EDIT: I forgot to mention, there's a bit more to this that occurred to me after I initially posted. You'll have to protect the fields from being edited in the event the server-side verification is successful as you obviously don't want the user changing values and then clicking the Confirm button. That means disabling all the input controls - which could be a pain if you have a lot. You also have to give them a way to (intentionally) Edit in case the server side verification passes, you display the Confirmation, and they change their minds - so basically you'd need a third "Cancel/Edit"-type button that would put the form back in edit mode and show your initial Submit button.
I have got a checkbox and want to display the confirmation message when it is clicked
I added the event binding in Code Behind file as below.
chkSMTLock.Attributes.Add("onclick", "return ConfirmSMTLock();");
The following is my HTML code for chkSMTLock
<asp:CheckBox ID="chkSMTLock" runat="server" AutoPostBack="true" OnCheckedChanged="chkSMTLock_CheckedChanged" Text="SMT Lock" />
Here is my javascript:
function ConfirmSMTLock() {
var r = confirm('Are you sure that you want to SMT lock/unlock this account?');
console.log(r);
return r;
}
When I run it, I can see the confirmation values (true/ false) in the browser console logs, but, it's not calling any server side code.
My server side code is very simple with logging...
protected void chkSMTLock_CheckedChanged(object sender, EventArgs e)
{
Utils.Debug("chkSMTLock_CheckedChanged");
}
When I remove javascript event binding for the checkbox, it executes the ServerSide event successfully. But When I put it back, it stops working.
How can I use the confirmation message box to control it?
Your validation is too far down the chain. As you've got AutoPostBack=true, you're basically submitting a form when clicking the checkbox, your validation wants to be at the form level.
Form.Attributes.Add("onclick", "return ConfirmSMTLock();");
And in ConfirmSMTLock() check the status of the checkbox to see if you need to fire the confirm dialogue. That's the simplest way I can think of.
On a side note: if you do this:
chkSMTLock.Attributes.Add("onclick", "return false;");
The checkbox becomes untickable
I have found out an answer. Since AutoPostBack = true, it will automatically send a post back to the server. So, first you need to delete that attribute from the html code.
<asp:CheckBox ID="chkSMTLock" runat="server" OnCheckedChanged="chkSMTLock_CheckedChanged" Text="SMT Lock" />
Then implement the PostBack behaviour by using the __doPostBack function of ASP.Net.
function ConfirmSMTLock() {
var r = confirm('Are you sure that you want to SMT lock/unlock this account?');
if (r == true)
{
__doPostBack("chkSMTLock", '');
return true;
}
return false;
}
Open Sasame!!!
protected void addSchoolButtonClick(object sender, ImageClickEventArgs e)
{
Page.ClientScript.RegisterStartupScript(GetType(), "MyKey1", "SchoolSearchPopUp();", true);
/*Some code*/
}
I am developing a website in asp.net.At a Hyperlink onclick event i want to call a javascript function"SchoolSearchPopUp()".this function is for creating a new popup window and it is working correctly.But my problem is ,a javascript function is calling or pop window opens only after executing the rest of the code in that function and that code need's some data that occurs as a result of popup.How can i create the popup before executing the rest of code in that function.
Change your postback trigger to something within the popup.
I don't think javascript can be called from code behind. C# is running from the server and java is all client side. There's a good explanation to a similar question here: http://forums.asp.net/t/1117189.aspx
If you need to execute a javascript function, you could try changing the hyperlink to a button and making use of the OnClientClick property. This executes script client side rather than calling a method on the server.
<asp:button id="Button1"
text="Click Here"
onclientclick="SchoolSearchPopUp()"
runat="server" />
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
You will need to write JavaScript on the page to handle the click of the button first and then call to the page method on the server.
Add an OnClientClick attribute to your button element and run your JavaScript method from there:
<asp:Button ID="TestButton" OnClientClick="SchoolSearchPopup()" Text="Click Me" OnClick="addSchoolButtonClick" runat="server"/>
<script type="text/javascript">
function SchoolSearchPopup()
{
alert("Popup");
}
</script>
If you want to execute some javascript before your postback you will need to register your hyperlink's click event to a js method, then submit your post to the server after performing whatever client side logic you are looking to run. (not the other way around, using RegisterStartupScript)
Example:
$("#myHyperLink").click(function() {
// do page logic, in your case show a modal window
$("#myModalDivContainer").show();
// submit your post to the server... replace targetClientID with ID of server control you're posting to
__doPostBack('targetClientID', '');
// NOTE: If you want to perform an AJAX request instead simply use some jQuery here instead. it's up to you how to handle the request from this point :)
});
Hope this helps!
Hi everyone I have a web form in which I am having a button on clicking which data back up is being taken, I used the following javascript :
<script language="javascript" type="text/javascript">
function showPleaseWait() {
document.getElementById('PleaseWait').style.display = 'block';
}
</script>
<asp:Button ID="btnTakebackup" runat="server" Text="Take Backup" Enabled="true"
onMouseDown="showPleaseWait()" CausesValidation="false" />
<div id="PleaseWait" style="display: none;">"Please Wait Backup in Progress.."</div>
Hi I am using a button to take a back up.
Now I want to show a message in btnTakebackup_Click() event, whether Back up was successful or not.
I used Response.Write("<script>alert('abcd');</script>"); in btnTakebackup_Click() event.
But the problem is that I want to show the page also, which is not showing instead white background is showing.
Thanks in advance...
To show a message box alert should be able to write out a new script to the response stream:
var script =
"<script type=\"javascript\">" +
"alert(\"Backup in progress, don't go!\");" +
"</script>"
Response.Write(script);
However much this is distasteful, I suppose it is sometimes "necessary".
You can add client side event handlers to ASP controls:
How to: Add Client Script Events to ASP.NET Web Server Controls
Cheers.
Do you really want it to be an alert? (You should know that they lock up the whole browser not just the tab your page is on), do your users really need to acknowledge the backup success by clicking ok or just be informed of it?...
I suggest you have a div on the page that says "Backup successful". The visibility of which can be set by a boolean property BackUpSuccess which you can set to true in the code behind you mention.
<div id="backUpSuccess" <%=BackUpSuccess ? "" : "style='display:none;'"%>>
Backup was successfull
</div>
...you can style the div as you like in your .css file to get attention.
If you really do want an alert you could run some JavaScript on page load to check the content of a hidden input that you set server side in similar fashion...but running javascript on page load is tricky...unless your using jQuery and then you will know it's very easy.
From your question, I understood that after clicking on the button, the data back up is happening, but the alert is not displaying as soon as you clicked the button.This is because you are calling the JavaScript in the button click event which will be fired only after all the code in the button click is executed.I suggest you to add a JavaScript function in the .aspx source page it self and call the JavaScript function as shown below:
<script ...>
function xyz()
{
alert('Please Wait');
}
</script>
and in button declaration
<asp:button id='btn_submit' runat="server" OnClientClick="return xyz();" />
Im using asp.net c# (webforms)
I want to add a back button to my page. (you land on this page if you incorrectly fill in a form).
if javascript is enabled i want to go back via javascript, but if it is disabled i'll just do a response.redirect("~/home.aspx")...
how can i implement this? is it 2 buttons? how can i hide the other in the 2 different states if so.
thanks
<asp:Button OnClick="serverMethod" OnClientClick="return(jsMethod);" runat="server" ID="redirectButton" Text="Back" />
on the button, but set the OnClientCLick to:
return(javascriptredirect());
and the OnClick to your button click event handler in your code behind.
if javascript is enabled, it will do the javascript redirect, if not enabled the page will post back and you can do your OnClick code behind method whic can to the response.redirect. You can also return false from your function to prevent the postback from occurring such as the case with setting the OnClientClick="return(confirm('Are you sure?'));"
This link guides you how to do it http://aspadvice.com/blogs/azamsharp/archive/2007/09/30/Check-If-the-JavaScript-is-Enabled-on-the-Client_2700_s-Browser.aspx
Happy coding !!!!!!