Disabling a button doesn't fire click event on server side - c#

I have an imagebutton in my aspx page like:
<asp:ImageButton ID="btnProcessPayment" ImageUrl="~/Images/process-payment.png" OnClientClick="return disableButton(this);"
runat="server" OnClick="btnProcessPayment_Click" />
This is my javascript function:
function disableButton(button) {
button.disabled = true;
return true;
}
As you can see in my javascript event handler I have disabled the button to prevent the user from click the button twice. However, even my server side event handler doesn't get fired due to this. What am I doing wrong?
Note: If I comment out this line button.disabled = true; all works out pretty well.

Disabling the button disables the form submit as well. You need to do the submit yourself:
function disableButton(button) {
button.disabled = true;
__doPostBack(<%= btnProcessPayment.ClientID %>,''); // need to manually submit
return true;
}
Updated as per Waqas suggestion!

You need to fire out serverside event of the button some thing like this
onclick="this.disabled=true;__doPostBack('buttonclientid','');"
or
function disableButton(button) {
button.disabled = true;
__doPostBack('buttonclientid','');
return true;
}

Use this and it worked for me
__doPostBack('<%= btnSubmit.ClientID %>', '');
where btnSubmit is the ID of my button and I wanted to trigger the server side click event for the button. It works.

Related

How to create an asp:button programmatically in c# behind with click handling

i write this code after many search but is not working
i need to create as asp:button programmatically and handling it
for(int i=0;i<DtShow.Rows.Count;i++)
{
Button btn = new Button
{
Text = "حذف",
ID = i.ToString(),
UseSubmitBehavior = false,
CommandArgument =i.ToString(),
CssClass = "btn btn-danger"
};
btn.Click +=new EventHandler(this.btn_Click);
lstAccessDgv.Rows[i].Cells[2].Controls.Add(btn);
}
protected void btn_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
int id = Convert.ToInt32(b.ID);
DtCode.Rows.RemoveAt(id);
DtShow.Rows.RemoveAt(id);
lstAccessDgv.Rows[id].Visible = false;
}
why not call btn_Click?
Unfortunately your question doesn't make it clear what information you're looking for, however, since you did make a specific query, I'll address that.
why not call btn_Click?
Because the btn_Click event handler hasn't been bound to the button's click event.
That's why you need to do this when you create the button:
btn.Click +=new EventHandler(this.btn_Click);
This executes btn_Click when the button is clicked.
A thing to keep in mind, though, is that this function will execute for every button in the list so you need to make sure it does work that specifically relates to the list item to which the button belongs.

Why doesn't asp.net button work?

I have used this behind asp button click function. It works on local system but not after begin deployed on server. Why ?
public void EmployeeDeActivation()
{
hdnfieldSessionPersonalInfoID.Value = "0";
Session["ExtraPersonalInfoID"] = 0;
Response.Redirect("EmployeeInformation.aspx", false);
}
.aspx code:
<asp:Button ID="btnEmployeeActivated" runat="server" Visible="false" OnClick="btnEmployeeActivated_Click"
CssClass="btn btn-rounded pull-right btnEmployeeActivated" />
i.e. when i click button when on local system, it hits then button event and refrehes the page but when it doesn't work like then button click never hits.
Update:
protected void btnEmployeeActivated_Click(object sender, EventArgs e)
{
try
{
EmployeeDeActivation();
}
catch (Exception ex)
{
throw;
}
}
Doesn't this method need to accept an event handler? E.g.
protected virtual void OnClick(
EventArgs e
)
Also, the part of code where you set your hidden is not needed as you redirect after.
Also it has the wrong name as it doesn't match the onclick name
Try to enable Trace and log on every method in the page. Try to visualize what your code is doing during postback.
Another useful tool is Glimpse.
Hope it helps!
asp button property "Visible" is set to false in your code. how come button is rendering at first place ?

Update an UpdatePanel when ModalDialog closes or on button event

I have two pages. The first one has one button and an UpdatePanel that contains an image.
The button uses the following code to show a ModalDialog:
window.showModalDialog('AjustarImagem.aspx',
null,
'status:no;
LOCATION: NO;
TOOLBAR=NO ;
DIRECTORIES: NO;
dialogWidth:250px;
dialogHeight:300px;
dialogHide:true;
help:no;
scroll:yes');
return false;");
What I need to do is to update the UpdatePanel when the ModalDialog closes, or when the click event of the ModalDialog button fires.
Use the __doPostBack() at the onclick javascript event of your button:
<script type="text/javascript" >
function ReloadPanel() {
//debugger;
// Realiza un postback parcial al panel de ajax.
__doPostBack('<%=UpdatePanel1.ClientID %>', parametro);
}
</script>
And add the load event of the update panel for process the request if you want:
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
// Get the parameter
string arg = Request.Form["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(arg)) return;
}
View this post: How to use __doPostBack()
Hope this helps.

Registering Javascript function that manipulates radwindow from ajaxified radgrid

I'm trying to open a radwindow client side with some registered script.
Question 1: Is there a reason radwindow wouldn't be found if executed on startup, if so why and how do I fix it?
Question 2: Whenever I ajaxify the radgrid the event no longer fires. This makes sense, as there is not a postback occurring, so the page never starts up. How do I get registered scripts to execute in an ajax enviornment?
---- Relevant Code Behind ----
protected void RadgridProjects_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Member")
{
Session["ProjectId"] = (e.Item as GridDataItem).GetDataKeyValue("ProjectId").ToString();
radGridProjectMembers.Rebind(); //Not the same grid!
ClientScriptManager cs = Page.ClientScript;
string js = "<script type='text/javascript'>ShowWindow()</script>";
cs.RegisterStartupScript(this.GetType(), "showwindow", js);
}
}
---- Javascript Function ----
function ShowWindow()
{
alert("code fired");
var radWin = $find("<%= RadWindow1.ClientID %>");
radWin.show();
radWin.moveTo(650, 450);
radWin.set_width(500);
radWin.set_height(400);
}
The window never opens, but my testing alert does fire. It should be noted that when I use a client event for the script the window DOES open.

Preventing rapid submission button clicks?

I found a solution like this but my onclick event is already tied to a code-behind handler:
MyButton.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(MyButton, "").ToString());
onclick="this.disabled=true;__doPostBack('MyContrl$MyButton','');"
My code:
<asp:imagebutton id="CheckoutBtn" runat="server" ImageURL="Styles/Images/submit.gif" onclick="CheckoutBtn_Click">
code-behind:
protected void CheckoutBtn_Click(object sender, ImageClickEventArgs e)
{
{
MyShoppingCart usersShoppingCart = new MyShoppingCart();
if (usersShoppingCart.SubmitOrder(User.Identity.Name) == true)
{
CheckOutHeader.InnerText = "Thank you.";
Message.Visible = false;
CheckoutBtn.Visible = false;
}
else
{
CheckOutHeader.InnerText = "Submission Failed - Please try again. ";
}
}
}
Disabling the Button serverside won't work, the Button will be disabled AFTER the PostBack, in this time the user can still click several times, disabling it in JavaScript this.disabled=true; is the only way to successfully do this.
Cases where you are trying to prevent the user from submitting a form multiple times are best handled using the Post/Redirect/Get pattern.
It is a very simple pattern and Wikipedia does a good job of explaining it:
http://en.wikipedia.org/wiki/Post/Redirect/Get
I assume you want to skip clicks that come within a certain TimeSpan of previous clicks. Create a class variable "DataTime LastClickTime", initially set to DateTime.MinValue. When you enter the click handler, check if DateTime.Now - LastClickTime > TimeSpan(...desired...) and if it isn't, exit the click handler with a return.
Try to disable the button with javascript instead of disabling it server side?
<asp:imagebutton id="CheckoutBtn" runat="server" ImageURL="Styles/Images/submit.gif" onclick="CheckoutBtn_Click" OnClientClick="this.disabled=true;">
One way is hide the button from Javascript and show some ajax loader image.
function btnClientClick()
{
document.getElementById('CheckoutBtn').style.display = 'none';
document.getElementById('dvLoader').style.display = '';
}

Categories

Resources