1

I'm creating button from code c#:

        Button btnAddCalculation = new Button();
        btnAddCalculation.ID = "btnAddCalculation";
        btnAddCalculation.Text = "Add count";
        btnAddCalculation.SkinID = "middleButton";
        btnAddCalculation.Visible = true;

        string sPath = String.Format(
            "WindowCalculationArenda.aspx?{0}={1}&TempGuid={2}&IdDocument={3}",
            SessionViewstateConstants.CalculationType,
            CalcType.New,
            Guid.NewGuid(),
            oEditedDocument.Id.ToString()
        );
        //          btnAddCalculation.Attributes.Add("onclick", @"window.open('InformationAboutAddAddr.aspx', '_blank', 'location=yes,height=500,width=450,scrollbars=yes,status=yes');");
        btnAddCalculation.Click += new EventHandler(btnClick_cl);
        btnAddCalculation.OnClientClick = clsCommonHelper.sGetWindowOpen(sPath, 1100, 500) + "return false;";

    public static string sGetWindowOpen(string sLink, int iWidth, int iHeight)
    {
        return "javascript:setTimeout(function(){ WindowOpen('" + sLink + "', " + iWidth + ", " + iHeight + "); }, 100); ";
    }

but in the client side the function OnClientClick does not work, when I click nothing happens. What Did I do wrong???


Generated HTML:

<input type="submit" name="ctl00$ctl00$Main$EditorMain$tabTabContainer$ctl00$Attr‌​433$btnAddCalculatio‌​n"  
value="Add count" 
id="ctl00_ctl00_Main_EditorMain_tabTabContainer_ctl00_Attr43‌​3_btnAddCalculation"  
disabled="disabled" class="blue_button" />
1
  • You need to find out why the button is disabled. Might be due to some JavaScript. Also check whether you need an input[type=submit] or whether another type is a better fit. This might be connected to the original problem. Maybe some JavaScript disables the submit button because the form is not valid yet. Depending on the selector, this might lead to all inputs of type submit being disabled.
    – Markus
    Commented Dec 4, 2017 at 10:49

3 Answers 3

1

For Button being disabled

If the dynamic button is added to a container that is itself disabled, then this dynamic button will also be disabled. To make this sure that button is added properly, use a new container in HTML (e.g. <asp:PlaceHolder>) and add button to that container from codebehind.

Also good to check following :-

When Creating the new button, use CausesValidation = false. This will avoid any RequiredFieldValidator getting fired when this button is clicked. RequiredFieldValidator also stops button from being clicked. e.g.

Button btnAddCalculation = new Button();
btnAddCalculation.ID = "btnAddCalculation";
btnAddCalculation.Text = "Add count";
btnAddCalculation.SkinID = "middleButton";
btnAddCalculation.Visible = true;
btnAddCalculation.Enabled = true;
btnAddCalculation.CausesValidation = false;
0

Fix this:

    Button btnAddCalculation = new Button();
    btnAddCalculation.ID = "btnAddCalculation";
    btnAddCalculation.Text = "Add count";
    btnAddCalculation.SkinID = "middleButton";
    btnAddCalculation.Visible = true;
    btnAddCalculation.Enabled = true;

    string sPath = String.Format(
        "WindowCalculationArenda.aspx?{0}={1}&TempGuid={2}&IdDocument={3}",
        SessionViewstateConstants.CalculationType,
        CalcType.New,
        Guid.NewGuid(),
        oEditedDocument.Id.ToString()
    );
    //          btnAddCalculation.Attributes.Add("onclick", @"window.open('InformationAboutAddAddr.aspx', '_blank', 'location=yes,height=500,width=450,scrollbars=yes,status=yes');");
    btnAddCalculation.Click += new EventHandler(btnClick_cl);
    btnAddCalculation.OnClientClick = clsCommonHelper.sGetWindowOpen(sPath, 1100, 500) + "return false;";

public static string sGetWindowOpen(string sLink, int iWidth, int iHeight)
{
    return "javascript:setTimeout(function(){ WindowOpen('" + sLink + "', " + iWidth + ", " + iHeight + "); }, 100); ";
}

By default the asp.net server will create the button as disabled unless you specify explicitly otherwise.

0

The ASP.NET code you provided looks ok; so I suspect the problem is related to a script that is run on the clientside. Maybe the submit-button is disabled because the form contents are not valid yet. In this case your button might be disabled, depending on the selector that is used to identify the submit-button.

To avoid this, change the behavior of the button so that it is rendered as an input of type button instead of submit. You can use the UseSubmitBehavior-property to achieve this:

btnAddCalculation.UseSubmitBehavior = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.