Javant
Javant

Reputation: 1019

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>

Upvotes: 1

Views: 977

Answers (3)

meJustAndrew
meJustAndrew

Reputation: 6603

You are not adding corectl the button to the web form. Try this way:

Button btnEdit = New Button();
btn.Edit.Click += btnEdit_Click;
frmMain.Controls.Add(btnSave)

As shown in this question too.

Upvotes: 0

Ashkan S
Ashkan S

Reputation: 11471

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

Upvotes: 1

Ann L.
Ann L.

Reputation: 13965

I believe your problem comes from the fact that ASP.NET doesn't know you created a button. As far as it's concerned, all you did was pump out some text to the page.

As a result, when you post back to the server, it doesn't know it needs to do anything on the server side when you click.

Try creating it as an object (a System.Web.UI.WebControls.Button) and adding that to your page's Controls collection. Be aware that you'll have to do this both on the initial page build and on postback: if the control doesn't exist after postback, events that were hooked up to it don't fire.

Getting it to appear in the middle of your table may require you to do your HTML table creation in some other way, such as building a WebControls Table object and adding that to your Controls collection.

Upvotes: 1

Related Questions