Create button OnClientClick asp net

492 Views Asked by At

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

There are 3 best solutions below

0
On BEST ANSWER

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
On

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
On

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;