Click here to Skip to main content
15,904,339 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey All,
trying to add some dynamic controls to HtmlTable in code behind but getting error on FindControl and the error is as follows
Object reference not set to an instance of an object.

And FindControl is causing this error
when i put control id manually in findcontrol it runs ok but when i use it with loop to find all controls it gives error.
i will deeply appreciate your help

Have a good one.

What I have tried:

C#
HtmlTable table = new HtmlTable();
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td = new HtmlTableCell();
            foreach (Control c in pnl.Controls)
            {
                TextBox temp_txt = (TextBox)pnl.FindControl(c.ID);  //causing error
                                td.Controls.Add(temp_txt);
                tr.Controls.Add(td);
                table.Controls.Add(tr);
            }            
            pnl.Controls.Add(table);
Posted
Updated 15-Oct-20 0:41am
Comments
BillWoodruff 15-Oct-20 3:41am    
see if this helps: https://stackoverflow.com/a/3731146

Change

C#
foreach (Control c in pnl.Controls)


to

C#
foreach (Control c in pnl.Controls.OfType<TextBox>())


The way asp.net works under the covers is that it converts your html into Literal controls so although your aspx page is like;

HTML
<asp:Panel id="pnl" runat="server">
    <p>Some text</p>
    <asp:Textbox id="txt1" runat="server"/>
</asp:Panel>


asp.net converts it to this when compiling

HTML
<asp:Panel id="pnl" runat="server">
    <asp:Literal id="literal1" runat="server"><p>Some text</p></asp:Literal>
    <asp:Textbox id="txt1" runat="server"/>
</asp:Panel>


It even does this for "non text" like new lines, spacing etc, so there are more controls in your panel than you think and the .OfType makes sure you only get the ones you are interested in.
 
Share this answer
 
Quote:
Object reference not set to an instance of an object.

Details about the error you see: NullReferenceException Class (System) | Microsoft Docs[^]
Quote:
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null

Possibly because:
1. You've forgotten to instantiate a reference type
2. You've forgotten to dimension an array before initializing it.
3. You get a null return value from a method, and then call a method on the returned type.
4. You're using an expression (for example, you're chaining a list of methods or properties together) to retrieve a value
5. You're enumerating the elements of an array that contains reference types, and your attempt to process one of the elements throws
6. Exception is thrown by a method that is passed null

Quote:
getting error on FindControl

This means that the name of the control is incorrect or not present in that control hierarchy where you are trying to find it.

foreach (Control c in pnl.Controls)
{
    TextBox temp_txt = (TextBox)pnl.FindControl(c.ID);  //causing error
    td.Controls.Add(temp_txt);
With above code, it's not clear on what you want to do actually. Above code is looping through all the controls present in the panel and then add them to a table and then in end add table back to panel. It's like controls are at two places. Then you have tr/td added to table per control in a loop. It's messed up - mostly this leads to mismatch of controls and error. Further, are all controls in panel a textbox that you cast into?
 
Share this answer
 
Comments
Aftab Iqbal Clips 15-Oct-20 4:55am    
yes these are all of same type controls.what i get is that its not good approach to add controls to panel first and then add them to table, should i add them to table first before adding them to panel.
but how can i add controls to table which already have presence on form or panel?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900