Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to set a counter on submit button and set a limit for example is 5 clicked only,
C#
public partial class registration : System.Web.UI.Page
{
    static int count = 0;

protected void Button1_Click(object sender, EventArgs e)
    {

if (count++ >5)
                {
                    Button1.Enabled = false;
                    TextBoxFN.Enabled = false;
                    TextBoxLN.Enabled = false;
                    TextBoxBD.Enabled = false;
                    TextBoxCP.Enabled = false;
                    TextBoxADRESS.Enabled = false;
                    TextBoxEMAIL.Enabled = false;
                    txtcaptcha.Enabled = false;
                    DropDownListCS.Enabled = false;
                    DropDownListGENDER.Enabled = false;

                    Response.Write("<script LANGUAGE='JavaScript' >alert('Sorry Registration is full')</script>");
Posted

1 solution

You are almost there...

You only need to change the incrementation to happen before comparison by moving the ++ before the value.

Another smaller thing, rather than using Response.Write to output JavaScript, user ScriptManager.RegisterClientScriptBlock ;)

Example:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    // Increment before compare
    if (++count > 5)
    {
        Button1.Enabled = false;
        TextBoxFN.Enabled = false;
        TextBoxLN.Enabled = false;
        TextBoxBD.Enabled = false;
        TextBoxCP.Enabled = false;
        TextBoxADRESS.Enabled = false;
        TextBoxEMAIL.Enabled = false;
        txtcaptcha.Enabled = false;
        DropDownListCS.Enabled = false;
        DropDownListGENDER.Enabled = false;
                
        // Register JavaScript items rather than using Response.Write
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertFull", "alert('Sorry Registration is full');", true);
    }
}
 
Share this answer
 
Comments
Member 11407981 29-Jan-15 22:33pm    
thanks for your response but the problem is still not limiting the submit button
Gideon van Dyk 29-Jan-15 22:35pm    
As far as can be determined from your post, you want the button to be disabled after 5 clicks, this code does exactly that.

Unless you would want to define the limit as say an attribute on the button?
Member 11407981 29-Jan-15 22:46pm    
sorry i for got to say when the submit button is clicked the page is redirect to another page
Gideon van Dyk 29-Jan-15 22:54pm    
So you are using a cross page posting I see... I will submit a possible other solution incorporating both server-side and JavaScript.
Sujith Karivelil 29-Jan-15 22:46pm    
Good You can include this also to the answer;
count++ : will use the value before increment
where as ++count : will increment the value before use

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900