Search code examples
c#asp.netasp.net-mvcserver-side

Dynamically Created Button refuses to call it's onClick


So while fooling around with webforms, I tried to create a page that outputs a table from a sql server to a html table on Page_Load. I then tried to add a button to a column which would redirect to a page. The only problem is, when I press the button nothing happens at all... I've tried putting breakpoints at the onclick method but they are never reached.

        num = ds.Tables[0].Rows.Count;
        htmlTable.Append("<tr style='background-color:#bd0000; color: White;'><th>ID</th><th>Job #</th><th>Project</th><th>Completed By</th><th>Date Created</th><th></th></tr>");

        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)              
            {
                int MAX_VIEW = (ds.Tables[0].Rows.Count > 15) ? 15 : ds.Tables[0].Rows.Count;
                for (int i = 0; i < MAX_VIEW; i++)
                {
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["CCPOF_ID"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Job_Number"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Project_Name"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["CompletedBy"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["DateCreated"] + "</td>");
                    htmlTable.Append("<td width=\"10%\"><button class=\"astext\" name=\"Btn" + ds.Tables[0].Rows[i]["CCPOF_ID"] + "\" id =\"" + ds.Tables[0].Rows[i]["CCPOF_ID"] + "\" OnClick =\"btnEdit_Click\" runat=\"server\" >Edit</button> | Details</td>");
                    htmlTable.Append("</tr>");
                }
                htmlTable.Append("</table>");
                DBDataPlaceHolder.Controls.Add(new System.Web.UI.WebControls.Literal { Text = htmlTable.ToString() });
            }
            else
            {
                htmlTable.Append("<tr>");
                htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                htmlTable.Append("</tr>");
            }
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        String id = ((System.Web.UI.WebControls.Button)sender).ID;
        Server.Transfer("CcpofDetails.aspx?ID=" + id);
    }
}

When I inspect the button in the live form this is what I see

<button class="astext" name="Btn10" id="10" onclick="btnEdit_Click" runat="server">Edit</button>

Solution

  • Your way of generating dynamic controls seems very strange to me. It is not the way web-forms work.

    To run an event on a control, it has to be loaded into servers memory first. But you are just filling some html text that is understandable only for the browser, not for asp.net engine.

    take a look at this sample

    To give you a better idea, create your buttons like this

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
            AddControls();
    }
    
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["controsladded"] == null)
        AddControls();
    }
    
    private void AddControls()
    {
        TextBox dynamictextbox = new TextBox();
        dynamictextbox.Text = "(Enter some text)";
        dynamictextbox.ID = "dynamictextbox";
        Button dynamicbutton = new Button();
        dynamicbutton.Click += new System.EventHandler(dynamicbutton_Click);
        dynamicbutton.Text = "Dynamic Button";
        Panel1.Controls.Add(dynamictextbox);
        Panel1.Controls.Add(new LiteralControl("<BR>"));
        Panel1.Controls.Add(new LiteralControl("<BR>"));
        Panel1.Controls.Add(dynamicbutton);
        ViewState["controlsadded"] = true;
    }
    
    private void dynamicbutton_Click(Object sender, System.EventArgs e)
    {
        TextBox tb = new TextBox();
        tb = (TextBox) (Panel1.FindControl("dynamictextbox"));
        Label1.Text = tb.Text;
    }