0

I have a TabContainer in my aspx page as follows

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

am creating the tabs for the above containter using C# code on Oninit event of the page

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

    }

}

now am trying to this on a btnclickevent

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

it just returns null. Am i doing something wrong? Kindly Help

Jaya
  • 3,721
  • 4
  • 32
  • 48
  • Possible duplicate: http://stackoverflow.com/questions/12076952/findcontrol-always-returns-null – FrostyFire Feb 08 '13 at 01:33
  • I beg to differ.. Not a duplicate. The dynamically created table is not in a custom template as in my case. – Jaya Feb 08 '13 at 01:50

2 Answers2

0

As i found the answer to the above question posted by myself, I would like to help fellow folks who encounter the same or similar problem.

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

Thank you "Stackoverflow" for maintaining the site and I also thank the members who help developers like me.

Jaya
  • 3,721
  • 4
  • 32
  • 48
0

Your issue isn't with the dynamically added controls, but that the FindControl method doesn't propogate and check all the way down the children stack.

I made a quick helper method below that checks the children until it finds the right control. It was a quick build, so it probably can be improved upon, I haven't tried but you could probably extend the control so you don't have to pass in an initial control. I tested it, however for some reason I couldn't get it to work passing in the Page object, I had to pass in the initial panel I used, but it should get the point across.

Control Finder

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

HTML Page

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

Button Click event

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