如何将动态创建的按钮添加到动态创建的表? [英] How to add dynamically created buttons to a dynamically created table?

查看:66
本文介绍了如何将动态创建的按钮添加到动态创建的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近一个问题真的困扰着我.我希望你能帮助我.

A question has been really bugging me lately. I hope you would help me.

我想在用户单击按钮后创建一个表.我要在第一个单元格的每一行上添加一个按钮.不幸的是,表格按钮无法触发事件.你能告诉我怎么做或我的错误在哪里吗?我已经使用本文来创建以下示例代码.

I want to create a table after a user clicks a button. On each row in the first cell I want to add a button. Unfortunately, the table buttons fail to fire an event. Could you tell me how to do it or where my mistake is? I have used this article to create the following example code.

<asp:PlaceHolder ID="placeHolderForTable" runat="server">

</asp:PlaceHolder>
<asp:Button ID="btnCreateTbl" runat="server" Text="Create Table"/>
<asp:Label ID="lblResult" runat="server" />

代码:

private struct ControlInfo
{
    public string ID;
    public string Type;
    public int Border;
    public HtmlTableRow[] TblRows;
    public HtmlTableCell[] TblCells;
    public int Width;
    public int Height;
}


protected void Page_Load(object sender, EventArgs e)
{
    btnCreateTbl.Click+=new EventHandler(BtnClick);
    if (this.IsPostBack) this.RecreatePersistedControls();
}//end Page_Load


//Call CreateControl for each persisted control
private void RecreatePersistedControls()
{
    ArrayList al = (ArrayList)this.Session["DynamicControls"];
    if (al != null)
        foreach (ControlInfo ci in al)
            this.CreateControl(ci);
}//end RecreatePersisterdControls method

//Create control specified by ControlInfo structure
private Control CreateControl(ControlInfo ci)
{
    Control ctl = null;

    switch (ci.Type)
    {
        case "Button":
            ctl = new Button();
            ctl.ID = ci.ID;
            ((Button)ctl).Text = "Edit";
            ((Button)ctl).Click+=new EventHandler(this.DoNothing);
            if (this.placeHolderForTable.FindControl("tblResult") != null)
            {
                for (int r = 0; r < ci.Height; r++)
                {
                    if (this.placeHolderForTable.FindControl("tblResult").FindControl("tr" + (r + 1)) != null && (r+1).ToString()==ci.ID[7].ToString())
                        if (this.placeHolderForTable.FindControl("tblResult").FindControl("tr" + (r + 1)).FindControl("td" + (r + 1) + "1") != null)
                            if(this.placeHolderForTable.FindControl("tblResult").FindControl("tr" + (r + 1)).FindControl("td" + (r + 1) + "1").FindControl(ctl.ID)==null)
                                this.placeHolderForTable.FindControl("tblResult").FindControl("tr" + (r + 1)).FindControl("td" + (r + 1) + "1").Controls.Add(ctl);
                }
            }
            break;
        case "HtmlTable":
            ctl = new HtmlTable();
            ctl.ID = ci.ID;
            ((HtmlTable)ctl).Border = ci.Border;

            for (int r = 0; r < ci.Height; r++)
            {
                HtmlTableRow row = ci.TblRows[r];
                row.ID = "tr" + (r + 1);
                for (int c = r * ci.Width; c < r * ci.Width+2; c++)
                {
                    ci.TblCells[c].ID="td"+(r+1)+(c%2+1);
                    row.Cells.Add(ci.TblCells[c]);

                }
                ((HtmlTable)ctl).Rows.Add(row);
            }
            if(this.placeHolderForTable.FindControl(ctl.ID)==null)
                this.placeHolderForTable.Controls.Add(ctl);
            break;
        default:
            return null;
    }
    return ctl;
}//end CreateControl method

//Create ControlInfo structure and persist it to Session
private ControlInfo PersistControl(string id, string type, int border, HtmlTableRow[] tblRows, HtmlTableCell[] tblCells, int width,
    int height)
{
    ControlInfo ci = new ControlInfo();
    ci.ID = id;
    ci.Type = type;
    ci.Border = border;
    ci.TblRows = tblRows;
    ci.TblCells = tblCells;
    ci.Width = width;
    ci.Height = height;

    ArrayList al = (ArrayList)this.Session["DynamicControls"];
    if (al == null) al = new ArrayList();
    al.Add(ci);
    this.Session["DynamicControls"] = al;
    return ci;
}//end PersistControl method

private void BtnClick(object sender, EventArgs e)
{
    int cellIx=0,rowIx=0;            
    HtmlTableRow tr1 = new HtmlTableRow();
    HtmlTableCell td11 = new HtmlTableCell();
    tr1.Cells.Add(td11);
    HtmlTableCell td12 = new HtmlTableCell();
    td12.InnerText = "td12";
    tr1.Cells.Add(td12);
    HtmlTableRow tr2 = new HtmlTableRow();
    HtmlTableCell td21 = new HtmlTableCell();
    tr2.Cells.Add(td21);
    HtmlTableCell td22 = new HtmlTableCell();
    tr2.Cells.Add(td22);
    td22.InnerText = "td22";
    HtmlTableRow []arrRows=new HtmlTableRow[2];
    arrRows[rowIx++]=tr1;
    arrRows[rowIx++]=tr2;
    HtmlTableCell []arrCells=new HtmlTableCell[4];
    arrCells[cellIx++]=td11;
    arrCells[cellIx++]=td12;
    arrCells[cellIx++]=td21;
    arrCells[cellIx++]=td22;
    ControlInfo ci = PersistControl("tblResult", "HtmlTable", 3, arrRows, arrCells, 2, 2);
    HtmlTable tblResult = (HtmlTable)CreateControl(ci);

    ci = PersistControl("btnEdit1", "Button", 0, arrRows, arrCells, 2, 2);
    Button btnEdit1 = (Button)CreateControl(ci);

    ci = PersistControl("btnEdit2", "Button", 0, arrRows, arrCells, 2, 2);
    Button btnEdit2 = (Button)CreateControl(ci);
}

public void DoNothing(object sender, EventArgs e)
{
    lblResult.Text = (sender as HtmlButton).ID + " done";
}

推荐答案

动态控件完全是另一种野兽,我尝试尽可能地远离它们.话虽如此,在某些情况下可能有必要.

Dynamic Controls are whole other beast and I try to stay away from them as much as possible. That being said, there are cases where they may be necessary.

关于事件处理程序和动态控件的事情,您必须确保在OnInit或Page_Load函数中连接控件并将其添加到页面控件树中,否则它们的行为会很奇怪

The thing about event handlers and dynamic controls, you have to be sure to wire up and add the control to the page control tree in the OnInit or Page_Load functions otherwise they can behave strangely http://support.microsoft.com/?id=317794

我对您的代码进行了一些编辑,以连接Page_Load处的事件处理程序.见下文,如果您有任何疑问,请告诉我:

I've made some edits to your code that wires up the event handlers at Page_Load. See below and let me know if you have any questions:

    // Added a ViewState persisted prop
    bool WireUpControls
    {
        get
        {
            return ViewState["wireUpControls"] != null ? Convert.ToBoolean(ViewState["wireUpControls"]) : false;
        }
        set
        {
            ViewState["wireUpControls"] = value;
        }
    }

    void CreateControlTable()
    {
        int cellIx = 0, rowIx = 0;
        HtmlTableRow tr1 = new HtmlTableRow();
        HtmlTableCell td11 = new HtmlTableCell();
        tr1.Cells.Add(td11);
        HtmlTableCell td12 = new HtmlTableCell();
        td12.InnerText = "td12";
        tr1.Cells.Add(td12);
        HtmlTableRow tr2 = new HtmlTableRow();
        HtmlTableCell td21 = new HtmlTableCell();
        tr2.Cells.Add(td21);
        HtmlTableCell td22 = new HtmlTableCell();
        tr2.Cells.Add(td22);
        td22.InnerText = "td22";
        HtmlTableRow[] arrRows = new HtmlTableRow[2];
        arrRows[rowIx++] = tr1;
        arrRows[rowIx++] = tr2;
        HtmlTableCell[] arrCells = new HtmlTableCell[4];
        arrCells[cellIx++] = td11;
        arrCells[cellIx++] = td12;
        arrCells[cellIx++] = td21;
        arrCells[cellIx++] = td22;
        ControlInfo ci = PersistControl("tblResult", "HtmlTable", 3, arrRows, arrCells, 2, 2);
        HtmlTable tblResult = (HtmlTable)CreateControl(ci);

        ci = PersistControl("btnEdit1", "Button", 0, arrRows, arrCells, 2, 2);
        Button btnEdit1 = (Button)CreateControl(ci);

        ci = PersistControl("btnEdit2", "Button", 0, arrRows, arrCells, 2, 2);
        Button btnEdit2 = (Button)CreateControl(ci);
    }

    // Call CreateControlTable() if we need to create the controls and wire up events
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCreateTbl.Click += new EventHandler(BtnClick);
        if (WireUpControls) CreateControlTable();
        //if (this.IsPostBack) this.RecreatePersistedControls();

    }

    protected void BtnClick(object sender, EventArgs e)
    {
       WireUpControls = true;
       CreateControlTable();
    }

    public void DoNothing(object sender, EventArgs e)
    {
        lblResult.Text = (sender as Button).ID + " done";
    }

这篇关于如何将动态创建的按钮添加到动态创建的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆