开发者

Page.FindControl comes back null for DIV

I have a page where I cre开发者_开发技巧ate div controls dynamiclly and number them automaticlly.

subCell = new TableCell();
subCell.Controls.Add(new LiteralControl(
    "<div id=\"picker" + Index.ToString() + "\" runat=\"server\" 
     class=\"colorSelector\"><div style=\"background-color: #000000;\">Text
     </div></div>"));
subRow.Cells.Add(subCell);
subTb.Rows.Add(subRow);

Later in the code I want to get the background-color value like so:

HtmlGenericControl div;

div = (HtmlGenericControl)Page.FindControl("picker" + e.CommandArgument.ToString());

string colorCode = div.Style["background-color"].ToString();

after these line of code I get a null object ref error. div is null. I have tried HtmlControl and LiteralControl as the object type and that does not help either.

Thank you!


Page.FindControl only works for server controls. You are assigning an id to text within the literal control which happens to be a div, but not the control itself. If you set the id of the control you should be able to find it, but I don't know if that's what you're intending.

In response to your comment, check your HTML. The runat=server will probably be there because ASP.NET isn't processing it as a control, it is treating it as content. Try something like this, noting that Page.FindControl only works on immediate children. Here I've declared a server panel named 'declaredPanel' in the aspx. ClientIdMode.Static makes it so ASP.NET won't add parent names to the control (like "MainContent_childPanel")

<asp:Panel ID="declaredPanel" runat="server" ClientIDMode="Static" />

In Page_Load:

    Panel p = new Panel();
    p.Style["background-color"] = "#aaeeaa";
    p.ID = "childPanel";
    p.ClientIDMode = System.Web.UI.ClientIDMode.Static;
    p.Controls.Add(new LiteralControl("<div id=\"div111\" runat=\"server\">Hello, world!</div>"));
    declaredPanel.Controls.Add(p);
    Panel p2 = declaredPanel.FindControl("childPanel") as Panel;
    string colorCode = p2.Style["background-color"]; // reports "#aaeeaa"

Produces this:

<div id="declaredPanel">
    <div id="childPanel" style="background-color:#aaeeaa;">
        <div id="div111" runat="server">Hello, world!</div>
    </div>
</div>


You have to add dynamic controls in OnInit event and event at postback. If not - all controls will be not visible at postback HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET

Note When you create dynamic controls on a Web Form, the controls must be created and added to the controls collection either in the OnInit or in the Page_Load events. Otherwise, the controls behave unexpectedly.


Use a Panel instead of a LiteralControl. It'll be rendered as a DIV and will be found by FindControl via it's ID. You can't find the div because it is "wrapped" inside the LiteralControl. Control.FindControl searches the current NamingContainer for a control(runat=server) with the given ID. Remember that FindControl won't find elements recursively(f.e. in all TableCells on a page).

Have a look here for a ... Better way to find control in ASP.NET


Try the following:

LiteralControl literalControl = new LiteralControl();
literalControl.ID = "divLiteralControl";
literalControl.Text = ...
subCell.Controls.Add(literalControl);

Then use the FindControl method to get the literal control and edit its text.

LiteralControl literalControl = 
    (LiteralControl) subCell.FindControl("divLiteralControl");
literalControl.Text = ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜