Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to be able to reference the placeholder so I can add a literal to placeholder.
The literal control will have string builder to hold data items.

And I've tried adding other controls from code behind but no matter which control I tried adding always returns null.

Any help is appreciated.

What I have tried:

Here's my code.

public void Forms_Inventory(string sql)
        {
            _ds = Utilities.Getds(sql);

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] {
                    new DataColumn("Name", typeof(string)),
                    new DataColumn("Department",typeof(string)) });
            dt.Rows.Add("Brian", "IT");
            dt.Rows.Add("Ed", "Help Desk");
            dt.Rows.Add("Peter", "Engineering");
            dt.Rows.Add("Scott", "Engineering");
            StringBuilder sb = new StringBuilder();
            //Add Table Header
            sb.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                sb.Append("<th style='background-color: #3366ff;border: 1px solid #000'>" + column.ColumnName + "</th>");
            }
            sb.Append("</tr>");
            //Add Table Rows
            foreach (DataRow row in dt.Rows)
            {
                sb.Append("<tr>");
                //Add Table Columns
                foreach (DataColumn column in dt.Columns)
                {
                    sb.Append("<td style='width:100px;border: 1px solid #ccc'>" + row[column.ColumnName].ToString() + "</td>");
                }
                sb.Append("</tr>");
            }

            PlaceHolder plc = new PlaceHolder();
            plc = (PlaceHolder)FindControl("plcInventory");
            plc.Controls.Add( new Literal { Text = sb.ToString() });
        }
Posted
Updated 8-May-21 8:16am

1 solution

FindControl method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

SO need to know you ASP.NET PAGE CODE ALSO.

Or you first check the code whether you are getting control from the FindControl method or not.
// Find control on page.
Control objmyControl = FindControl("YourcontrolId");
 
Share this answer
 

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