how do I disable Button asp.net - asp.net

How can I disable or enable button in asp.net? I want to disable button after click to prevent double click. I am trying to disable my Login button after clicking on it.

You have to disable it on client so that user could not click it again.
<asp:button id="btn" runat="server" OnClientClick="this.disabled=true;"......
To disable on server side asp.net code.
btn.Enabled = false;

You can use the client-side onclick event to do that:
yourButton.Attributes.Add("onclick", "this.disabled=true;");
or
You can do this with javascript. in your form tag,
onsubmit="javascript:this.getElementById("submitButton").disabled='true';"
or
In code behind file you can do like this
button1.enabled = false

write a java-script function which checks the user name and password.If they are not blank the disable the button.
But if you disable the button and there is a postback. And after the postback still it will be enable.
So Idea is
Create a java-script function.
validate user-name and password
if they are valid
disable the button (javascript).
Add ClientIdMode="Static" to your <asp:button> to prevent .NET from mangling the name.
--edit
<asp:button id="btn" runat="server" ClientIdMode="Static" OnClientClick="return btn_disable" ...
Your java-script code
function btn_disable
{
//check for user name and password
// if filled
document.getElementById("btn").disabled=true;
}

Front-end only:
<button id="loginbtnID" onclick="DisableBtn()">Log in</button>
<script type="text/javascript">
function DisableBtn() {
document.getElementById("loginbtnID").disabled = true;
}
</script>
Front-end & Code Behind:
(this reloads the whole page though.. check this to prevent reloading)
disablebutton.aspx:
<button id="loginbtnID" runat="server" onclick="DisableBtn()" OnClick="codeBehindFunction">Log in</button>
disablebutton.aspx.cs:
protected void codeBehindFunction(object sender, EventArgs e)
{
loginbtnID.Disabled = false;
}

Related

OnClick event not hitting the back end ASP.NET

I have a button that looks like this with both OnClick and OnClientClick events
<asp:Button ID="btnExecute" CssClass="btn btn-primary" runat="server" Text="Execute" OnClientClick="setHiddenValues();" OnClick="Execute_Click" />
<script type="text/javascript">
function setHiddenValues() {
//set some values
}
</script>
EDIT: the code behind looks like this.
protected void Execute_Click(object sender, EventArgs e)
{
//execute some code
Response.Redirect("LandingPage");
}
Now when I click on the 'Execute' button, it only seems to go to the javascript and doesn't hit break points in the backend. It just redirects me to an empty page. There are no errors in js console of the browser's dev tools and also no errors being thrown in the backend. Network tab in the dev tools shows a 200 but I know the back end is not hit. What am I doing wrong here?
try:
<asp:Button ID="btnExecute" CssClass="btn btn-primary"
runat="server" Text="Execute"
OnClientClick="setHiddenValues();return true;" OnClick="Execute_Click" />
On client click has to return a value of true, else the Onclick will not trigger.
Try configure this in Page element:
<%#Page AutoEventWireup="true" EnableEventValidation="false" %>
It turns out my issue was much simpler. The 'Debug' option was not selected. The wiring was correct and it was hitting the backend but I was not able to hit the breakpoints in the backend because I was in Release mode.

Form data is saving and validation message is also displaying

We are using the callbackpanel for the validation of the Devexpress controls but what actually happening is :
If we click submit button without entering any thing in textboxes in the form. It is showing the validation messages but it is also saving the blank data to the database.
I want to know if validation message is showing then why event in backend is calling for saving data ?
Basically in DevExpress ASPxCallbackPanel, a Button Click does not trigger a Callback, it triggers a Postback that is the difference between an UpdatePanel and a CallbackPanel.
So you have to call the ASPxCallbackPanelClient.PerformCallback('PARAM'); instead.
You did not mention how you trigger the callback, you did not show how your ASPxButton code looks or how your ASPxCallbackPanel looks or how your Validator looks, so considering that please see code below that works very well in you scenario
All the controls as DX, including validators and buttons
<dx:ASPxCallbackPanel runat="server" ID="cbpDIActions" ClientInstanceName="cbpDIActions" OnCallback="cbpDIActions_Callback">
<panelcollection>
<dx:PanelContent>
<dx:ASPxSpinEdit ID="txtBuilFixtNoBathrooms" runat="server" ValidationSettings-RequiredField-IsRequired="true" ValidationSettings-ValidationGroup="SubmitValidation">
</dx:ASPxSpinEdit>
<dx:ASPxButton ID="btn" runat="server" AutoPostBack="false" Text="Process" UseSubmitBehavior="False"
ValidationSettings-ValidationGroup="SubmitValidation" CausesValidation="true"
>
<ClientSideEvents Click="function(s,e){
ASPxClientEdit.ValidateGroup('SubmitValidation');
if (ASPxClientEdit.AreEditorsValid()) {
if(!cbpDIActions.InCallback()) {
cbpDIActions.PerformCallback('PARAM');}
}
}" />
</dx:ASPxButton>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
And in the backend you put all your code in the callback event
protected void cbpDIActions_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
{
if (e.Parameter != null && e.Parameter.ToString() == "PARAM")
{
//PROCESS YOUR CODE
}
}

Disable Button after click in dnn

for one requirements i need to disable button after click and run server side code after disabled for this i have used below code which is called at pageload
btnGREntry.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(btnGREntry, "") +
";this.value='Please wait...';this.disabled = true;");
but it is giving me below error.
An unknown error occurred while processing the request on the server.
The status code returned from the server was: 0
please help me to find out a solution or suggest any other solution to disable button after click
Note: similar things are working in asp.net but i am using in *dotnetnuke version 7.0 *
You can do this in the code-behind.
ASPX
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button" />
Code-behind
protected void Button1_Click1(object sender, EventArgs e)
{
Button a = (Button)sender;
a.Enabled = false;
}
Does the button have to remain disabled continuously or is it to return to enabled after page refresh? In the former case then handle it with both JavaScript and code behind. In the later case handle it with just JavaScript.
<asp:Button ID="Button1" runat="server" onClientClick="DisableButton();" onclick="Button1_Click1" Text="Button" />
JavaScript:
function DisableButton() {
var btn = $("#buttonID").attr("disabled", "disabled");
}

asp:Button wont fire onclick event

I have a button like
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="RedirectToLocker"
OnClientClick="return false;" UseSubmitBehavior="false" />
and a method in the codebehind like
public void RedirectToLocker(object sender, EventArgs e)
{
Response.Redirect(ConfigurationManager.AppSettings["LockerLoginURL"]);
}
But when I click the button it doesn't hit the code in the method. If I remove OnClientClick = "return false;" then the button submits the form and goes to the wrong page. What am I doing wrong?
EDIT: I am using the OnClientClick="return false;" code because without it the button for some reason acts as a submit for the form it is nested in and immediately redirects to the form action url instead of hitting my code.
When a clientside event handler returns false, the postback is omitted.
OnClientClick="return false;" // <-- no postback
So you either need to remove it or tell us why you actually want to return false from the js-onclick event. If the Response.Redirect goes to the wrong page, you might want to change that.
Edit: So you're redirecting to another page by setting the form's Action to another url. Then you could set the Button's PostBackUrl to the same url as the current page. Then it would hit the codebehind.
You need to remove OnClientClick="return false;" to run the server side method.
If the browser is redirecting to the wrong page, then check your LockerLoginURL application setting value in the web.config. This should match the URL you are being redirected to.
Change this to the correct URL.
Remove the OnClientClick="return false;"

how to open a page in new tab on button click in asp.net?

I want to open a page in new tab of browser on button click.
I have searched a lot on google but i couldn't find anything.
Here is my button.
<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="btnNewEntry_Click" />
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect("New.aspx");
}
Can you please help me how i can do this ?
You could use window.open. Like this:
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(
this.GetType(),"OpenWindow","window.open('YourURL','_newtab');",true);
}
Why not just call window.open straight from OnClick?
<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="window.open('New.aspx')" />
Try This
Link
Take care to reset target, otherwise all other calls like Response.Redirect will open in a new tab, which might be not what you want.
<asp:LinkButton OnClientClick="openInNewTab();" .../>
In javaScript:
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
Use JavaScript for the main form / Button click event. An example is:
Context.Response.Write("<script language='javascript'>window.open('AccountsStmt.aspx?showledger=" & sledgerGrp & "','_newtab');</script>")
try this rather than redirect...
Response.Write("<script>");
Response.Write("window.open('ClickPicture.aspx','_blank')");
Response.Write("</script>");
Just had the same problem. Client-side wasn't appropriate because the button was posting back information from a listview.
Saw same solution as Amaranth's on way2coding but this didn't work for me.
However, in the comments, someone posted a similar solution that does work
OnClientClick="document.getElementById('form1').target ='_blank';"
where form1 is the id of your asp.net form.
You have to use Javascript since code behind is server side only. I am pretty sure that this works.
<asp:Button ID="btnNewEntry" runat="Server" CssClass="button" Text="New Entry" OnClick="btnNewEntry_Click" OnClientClick="aspnetForm.target ='_blank';"/>
protected void btnNewEntry_Click(object sender, EventArgs e)
{
Response.Redirect("New.aspx");
}
Add this Script
<script type = "text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
and
<asp:Button ID="BTNpRINT" runat="server" Text="PRINT" CssClass="btn btn-primary" OnClick="BTNpRINT_Click" OnClientClick = "SetTarget();"/>
and
protected void BTNpRINT_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("~/Print.aspx?ID={0}",txtInv.Text));
}
You can add to your button OnClientClick like so:
<asp:Button ID="" runat="Server" Text="" OnClick="btnNewEntry_Click" OnClientClick="target ='_blank';"/>
This will change the current form's target for all buttons to open in new tab. So to complete the fix you can then use 2 approaches:
For any other button in this form, add to client click a "reset form target" function like so:
function ResetTarget() {
window.document.forms[0].target = '';
}
Add the same code inside the function inside a setTimeout() so the code will reset the form's target after few moments. See this answer https://stackoverflow.com/a/40682253/8445364
Per Open a URL in a new tab (and not a new window) using JavaScript
Nothing an author can do can choose to open in a new tab instead of a new window.
The browser decides between opening a new tab or opening a new window. You cannot control this as a developer.
In vb.net either on button click or on link button click, this will work.
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "openModal", "window.open('CertificatePrintViewAll.aspx' ,'_blank');", True)
add target='_blank' after check validation :
<asp:button id="_ButPrint" ValidationGroup="print" OnClientClick="if (Page_ClientValidate()){$('form').attr('target','_blank');}" runat="server" onclick="ButPrint_Click" Text="print" />
You could do this on the ASPX HTML front end to make the button go to a new tab to show page in your ASP.NET site dynamically:
<asp:Button ID="btnNewEntry" CssClass="button" OnClientClick="window.open('https://website','_blank'); return false;" text="WebsiteName" runat="server" />
If the url is dynamic and you want to control it in the code behind
<asp:Button ID="btnNewEntry" runat="Server" Text="New Entry" />
//code behind
var id = 0;
btnNewEntry.OnClientClick = $"window.open('New.aspx?ID={id}')";
A simple solution:
<a href="https://www.google.com" target="_blank">
<button type="button">Open new tab</button>
</a>
You shuld do it by client side. you can place a html hyperlink with target="_blank" and style="display:none".
after that create a javascript function like following
function openwindow(){
$("#hyperlinkid").click();
return false;
}
use this function as onclientclick event handler of the button like onclientclick="return openwindow()"
You need to include a jquery in the page.
Add_ supplier is name of the form
private void add_supplier_Load(object sender, EventArgs e)
{
add_supplier childform = new add_supplier();
childform.MdiParent = this;
childform.Show();
}

Resources