0

我的 aspx 页面中有一个 TabContainer,如下所示

<asp:TabContainer ID="tabcontainer" runat="server" ActiveTabIndex="0">
    </asp:TabContainer>

我在页面的 Oninit 事件上使用 C# 代码为上述容器创建选项卡

protected override void OnInit(EventArgs e)
{
    lstCategories = Service.GetCategories();
    numberOfCategories = lstCategories.Count;
    CreateTabs();
    base.OnInit(e);
}

protected void CreateTabs()
{
    try
    {
        for (int i = 0; i < numberOfCategories; i++)
        {
            TabPanel asptab = new TabPanel();
            asptab.ID = lstCategories[i].Id.ToString();
            asptab.HeaderText = lstCategories[i].Name;
            MyCustomTemplate obj = new MyCustomTemplate(lstCategories[i].Id);
            asptab.ContentTemplate = obj;
            tabcontainer.Tabs.Add(asptab);
        }
    }
    catch (Exception ex)
    {

    }
}
public class MyCustomTemplate : ITemplate
{
    public Table tbl;
    public TextBox tbxQuantity;
    public Image img;
    public int countOfItemsPerRow = 2;
    public MyCustomTemplate(int paramCategoryID)
    {
        categoryID = paramCategoryID;

    }

    public void InstantiateIn(Control container)
    {
        InitialiseTheProperties();
        container.Controls.Add(tblHardware);
    }
    public Table InitialiseTheProperties()
    {

        //Intialize the Mater Table 
        tbl = new Table();

        //Create Row for the mater Table
        TableRow row = new TableRow();
        TableCell cell = new TableCell();

            img = new Image();
            img.ImageUrl = HttpRuntime.AppDomainAppVirtualPath +"/Images/"+"1.jpg";
            cell.Controls.Add(img);
        tblHardware.Rows.cells.add(cell);
            tbxQuantity = new TextBox();
            tbxQuantity.ID ="TbxQuantity";
            cell.Controls.Add(tbxQuantity);
    tblHardware.Rows.cells.add(cell);
            tblHardware.Rows.Add(row);
    //return tbl;
        }

    }

}

现在正在尝试在 btnclickevent 上进行此操作

public void btnSave_Click(object sender, EventArgs e)
{
   try
   {
     Control cntrl = Page.FindControl("TbxQuantity");
   }
   catch (Exception ex)
   {
   }
}

它只是返回空值。难道我做错了什么?请帮助

4

2 回答 2

0

当我找到自己发布的上述问题的答案时,我想帮助遇到相同或相似问题的同胞。

string strQuantity=((System.Web.UI.WebControls.TextBox)(((AjaxControlToolkit.TabContainer)(BTN.Parent.FindControl("tabcontainer"))).Tabs[0].FindControl("TbxQuantity"))).Text

感谢“Stackoverflow”维护网站,也感谢帮助像我这样的开发人员的成员。

于 2013-02-08T01:46:08.390 回答
0

您的问题不在于动态添加的控件,而是 FindControl 方法不会传播并一直检查子堆栈。

我在下面做了一个快速的帮助方法,检查孩子直到找到正确的控制。这是一个快速构建,所以它可能可以改进,我没有尝试过,但您可能可以扩展控件,因此您不必传递初始控件。我对它进行了测试,但是由于某种原因,我无法让它在传递 Page 对象中工作,我必须传递我使用的初始面板,但它应该明白这一点。

控制查找器

public static class ControlFinder
{
    public static Control Find(Control currentControl, string controlName)
    {
        if (currentControl.HasControls() == false) { return null; }
        else
        {
            Control ReturnControl = currentControl.FindControl(controlName);
            if (ReturnControl != null) { return ReturnControl; }
            else
            {
                foreach (Control ctrl in currentControl.Controls)
                {
                    ReturnControl = Find(ctrl, controlName);
                    if (ReturnControl != null) { break; }
                }
            }

            return ReturnControl;
        }
    }
}

网页

<asp:Panel ID="pnl1" runat="server">
    <asp:TextBox ID="pnl1_txt1" runat="server" />
    <asp:TextBox ID="pnl1_txt2" runat="server" />

    <asp:Panel ID="pnl2" runat="server">
        <asp:TextBox ID="pnl2_txt1" runat="server" />
        <asp:TextBox ID="pnl2_txt2" runat="server" />

        <asp:Panel ID="pnl3" runat="server">
            <asp:TextBox ID="pnl3_txt1" runat="server" />
            <asp:TextBox ID="pnl3_txt2" runat="server" />
        </asp:Panel>
    </asp:Panel>
</asp:Panel>

<asp:Button ID="btnGo" Text="Go" OnClick="btnGo_Click" runat="server" />

<asp:Panel ID="pnlResults" runat="server">
    <div>pnl1_txt1: <asp:Label ID="lblpnl1txt1" runat="server" /></div>
    <div>pnl1_txt2: <asp:Label ID="lblpnl1txt2" runat="server" /></div>
    <div>pnl2_txt1: <asp:Label ID="lblpnl2txt1" runat="server" /></div>
    <div>pnl2_txt2: <asp:Label ID="lblpnl2txt2" runat="server" /></div>
    <div>pnl3_txt1: <asp:Label ID="lblpnl3txt1" runat="server" /></div>
    <div>pnl3_txt2: <asp:Label ID="lblpnl3txt2" runat="server" /></div>
    <div>unknown: <asp:Label ID="lblUnknown" runat="server" /></div>
</asp:Panel>

按钮点击事件

    protected void btnGo_Click(object sender, EventArgs e)
    {
        Control p1t1 = ControlFinder.Find(pnl1, "pnl1_txt1");
        Control p1t2 = ControlFinder.Find(pnl1, "pnl1_txt2");
        Control p2t1 = ControlFinder.Find(pnl1, "pnl2_txt1");
        Control p2t2 = ControlFinder.Find(pnl1, "pnl2_txt2");
        Control p3t1 = ControlFinder.Find(pnl1, "pnl3_txt1");
        Control p3t2 = ControlFinder.Find(pnl1, "pnl3_txt2");
        Control doesntexist = ControlFinder.Find(pnl1, "asdasd");

        lblpnl1txt1.Text = p1t1 != null ? "Found: " + p1t1.ID : "Not found";
        lblpnl1txt2.Text = p1t2 != null ? "Found: " + p1t2.ID : "Not found";
        lblpnl2txt1.Text = p2t1 != null ? "Found: " + p2t1.ID : "Not found";
        lblpnl2txt2.Text = p2t2 != null ? "Found: " + p2t2.ID : "Not found";
        lblpnl3txt1.Text = p3t1 != null ? "Found: " + p3t1.ID : "Not found";
        lblpnl3txt2.Text = p3t2 != null ? "Found: " + p3t2.ID : "Not found";
        lblUnknown.Text = doesntexist != null ? "Found: " + doesntexist.ID : "Not found";
    }
于 2013-02-08T04:14:11.580 回答