javascriptc#asp.netbuttononclientclick

Create button OnClientClick asp net


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" />

Solution

  • 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;