FindControl() return null

cadi2108 picture cadi2108 · Dec 21, 2011 · Viewed 12.6k times · Source

I trying to create application whad add controlls dynamicaly. I have masterpage, my asp:Content is here:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server">
</asp:ScriptManager>   
<div style="margin: 10px">
    <asp:UpdatePanel ID="updatePanel1" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder runat="server" ID="myPlaceHolder" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
</div>
<asp:Button ID="btnAdd" runat="server" Text="Add" />

After click in btnAdd I want to add two textboxes. I trying do it like in http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/

This is my code:

    static int myCount = 1;
    private TextBox[] color;
    private TextBox[] text;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        color = new TextBox[myCount];
        text = new TextBox[myCount];

        for (int i = 0; i < myCount; i++)
        {
            TextBox tbColor = new TextBox();
            tbColor.ID = "colorTextBox" + i.ToString();
            myPlaceHolder.Controls.Add(tbColor);
            color[i] = tbColor;

            TextBox tbText = new TextBox();
            tbText.ID = "textTextBox" + i.ToString();
            myPlaceHolder.Controls.Add(tbText);
            text[i] = tbText;

            LiteralControl literalBreak = new LiteralControl("<br />");
            myPlaceHolder.Controls.Add(literalBreak);
        }
    }


    public Control GetPostBackControl(Page page)
    {
        Control control = null;
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control mycontrol = page.FindControl(ctl);
                if (mycontrol is System.Web.UI.WebControls.Button)
                {
                    control = mycontrol;
                    // This gives you ID of which button caused postback                        
                    break;
                }
            }
        }
        return control;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Control myControl = GetPostBackControl(this.Page);
        if (myControl != null)
            if (myControl.ClientID.ToString() == "btnAdd")
                myCount = myCount + 1;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //handled in PreInit    

    }

When in function GetPostBackControl() in loap foreach looking for my btnAdd, for example in first iteration for ctr "ctl00$MainContent$scriptManager1", myControl is null... In next iterations also... So my function always return null. What can be reason?

Answer

StuartLC picture StuartLC · Dec 21, 2011

FindControl only searches direct children of the container. Since you are starting off at the page level, you will need to recurse through the child UpdatePanel control to get to your btnAdd control.

Have a look here for an example how to do do this.

Edit: I'm not sure I understand why you are 'looking' for your button in this manner, since there is only one static button on the screen - you wouldn't need to use FindControl in this case.

<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" />

(or in code, btnAdd.OnClick += new EventHandler(btnAdd_Click);)

Even if you had multiple Buttons in your form added dynamically, you could wire ALL of them up to the same Button Click handler, in which case sender would then contain the Button Control which was clicked. You would typically use FindControl to scrape the data out of the dynamically added Input controls (text box etc), rather than to see which control caused the Postback (as 'sender' in an appropriate event handler would be easier)

Edit 2: You can add the buttons dynamically just like your other controls

    Button myButton = new Button();
    myButton.Text = "Click Me";
    myButton.Click += new EventHandler(btnAdd_Click);
    myPlaceHolder.Controls.Add(myButton); 

If you want all the controls that you've added already to 'stay' in between postbacks then enable viewstate on the page and on the controls, and then make sure that you only add the controls once without postback, in OnInit:

   base.OnInit(e);    
   if (!IsPostBack)
   { // ... Add controls here

You can keep the state of 'mycount' in a hidden field (in the same updatepanel, and with viewstate enabled) - you'll need to parse it to an int each time. Or you can use SessionState to track it.