ephaltes ephaltes - 28 days ago 11
HTML Question

Can´t find my Control with FindControl

I am working on my website and have a problem.
I am adding controls dynamic into a contentplaceholder from the master page.
Then when i want to access the items I cant find them the Findcontrol method but the controls are there, because i can see them.

MASTER PAGE:

<body>
<form id="form1" runat="server">

<header id="master_headline" class="headline">Home Smart Home </header>

<nav>

<ul id="nav_hori" runat="server">
<asp:ContentPlaceHolder ID="master_navigation_hori" runat="server">
</asp:ContentPlaceHolder>
</ul>

</nav>

<nav>

<ul id="nav_vert" runat="server">
<asp:ContentPlaceHolder ID="master_navigation_vert" runat="server">
</asp:ContentPlaceHolder>
</ul>

</nav>


<div id="master_content_div">
<asp:ContentPlaceHolder ID="master_content_body" runat="server">
</asp:ContentPlaceHolder>



</div>

</form>



Somehow the end tag for the body isnt showing but whatever.

Code behind the Master Page:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
master_navigation_hori.Controls.Add(new LiteralControl(" <li><a href='test.aspx'>" + splitted[1] + "</a></li> "));
//master_navigation_hori.Controls.Add(new LiteralControl("< li class='divider -vertical'></li>"));
master_navigation_hori.Controls.Add(new LiteralControl(" <li><a href='test.aspx'>" + splitted[2] + "</a></li> "));
master_navigation_hori.Controls.Add(new LiteralControl(" <li><a href='test.aspx'>" + splitted[3] + " </a></li> "));
master_navigation_hori.Controls.Add(new LiteralControl("<li class='floatright'><a href='test.aspx'>&#9881</a></li>"));
master_navigation_hori.Controls.Add(new LiteralControl("<li class='floatright'><a href='test.aspx'>" + Session["username"] + "</a></li>"));
}
else
{
master_navigation_hori.Controls.Add(new LiteralControl(@"<li class='floatright' id='master_login'>
<a id='master_login-trigger' href='#'>Log in <span>▼</span> </a>
<div id='master_login-content' runat='server'>
<fieldset id='master_inputs'>
<input id='master_input_username' runat='server' type='text' name='username' placeholder='Username' value='test' required='required'/>
<input id='master_input_password' runat='server' type='password' name='password' placeholder='Enter your Password'/>
</fieldset>
<fieldset id='master_actions'>
<input type='submit' id='master_sub_login' runat='server' value='Log in' />
<label><input type='checkbox' id='master_staylogin' runat='server' checked='checked'/> Keep me signed in</label>
</fieldset>
</div>
</li>"));

}

}


Content Page Code behind:

protected void Page_Unload(object sender, EventArgs e)
{
if (Page.FindControl("master_login-content") != null)
MessageBox.Show("page");
if( Master.FindControl("master_login-content")!=null)
MessageBox.Show("master");
if ((Master.FindControl("master_navigation_hori").FindControl("master_login-content"))!=null)
MessageBox.Show("combi");
if(FindControl("master_login-content")!=null)
MessageBox.Show("nichts");

MessageBox.Show("end");

}

protected void Page_Load(object sender, EventArgs e)
{
if (Page.FindControl("master_login-content") != null)
MessageBox.Show("page");
if( Master.FindControl("master_login-content")!=null)
MessageBox.Show("master");
if ((Master.FindControl("master_navigation_hori").FindControl("master_login-content"))!=null)
MessageBox.Show("combi");
if(FindControl("master_login-content")!=null)
MessageBox.Show("nichts");

MessageBox.Show("end");

}


This returns everywhere null, it just shows the "end" MEssageBox.
the only thing its find is the master_navigation_hori

Regards

Answer

I'm already test your code. You must use System.Web.UI.HtmlControls.HtmlGenericControl instead of LiteralControl.

So, i make a change like this

System.Web.UI.HtmlControls.HtmlGenericControl master_login = new System.Web.UI.HtmlControls.HtmlGenericControl("LI");
master_login.ID = "master_login";

System.Web.UI.HtmlControls.HtmlGenericControl master_login_trigger = new System.Web.UI.HtmlControls.HtmlGenericControl("A");
master_login_trigger.ID = "master_login-trigger";
master_login_trigger.Attributes.Add("href", "#");
master_login_trigger.InnerHtml = "Log in <span>▼</span>";

master_login.Controls.Add(master_login_trigger);

System.Web.UI.HtmlControls.HtmlGenericControl master_login_content = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
master_login_content.ID = "master_login-content";

System.Web.UI.HtmlControls.HtmlGenericControl master_inputs = new System.Web.UI.HtmlControls.HtmlGenericControl("FIELDSET");
master_inputs.ID = "master_inputs";

System.Web.UI.HtmlControls.HtmlGenericControl master_input_username = new System.Web.UI.HtmlControls.HtmlGenericControl("INPUT");
master_input_username.ID = "master_input_username";
master_input_username.Attributes.Add("type", "text");
master_input_username.Attributes.Add("name", "username");
master_input_username.Attributes.Add("placeholder", "Username");
master_input_username.Attributes.Add("value", "text");
master_input_username.Attributes.Add("required", "required");

master_inputs.Controls.Add(master_input_username);

System.Web.UI.HtmlControls.HtmlGenericControl master_input_password = new System.Web.UI.HtmlControls.HtmlGenericControl("INPUT");
master_input_password.ID = "master_input_password";
master_input_password.Attributes.Add("type", "password");
master_input_password.Attributes.Add("name", "username");
master_input_password.Attributes.Add("placeholder", "Enter your Password");

master_inputs.Controls.Add(master_input_password);

master_login_content.Controls.Add(master_inputs);

System.Web.UI.HtmlControls.HtmlGenericControl master_actions = new System.Web.UI.HtmlControls.HtmlGenericControl("FIELDSET");
master_actions.ID = "master_actions";

System.Web.UI.HtmlControls.HtmlGenericControl master_sub_login = new System.Web.UI.HtmlControls.HtmlGenericControl("INPUT");
master_sub_login.ID = "master_sub_login";
master_sub_login.Attributes.Add("type", "submit");
master_sub_login.Attributes.Add("value", "Log in");

master_actions.Controls.Add(master_sub_login);

System.Web.UI.HtmlControls.HtmlGenericControl master_staylogin_label = new System.Web.UI.HtmlControls.HtmlGenericControl("LABEL");
master_staylogin_label.ID = "master_staylogin_label";
master_staylogin_label.InnerText = "Keep me signed in";

System.Web.UI.HtmlControls.HtmlGenericControl master_staylogin = new System.Web.UI.HtmlControls.HtmlGenericControl("INPUT");
master_staylogin.ID = "master_staylogin";
master_sub_login.Attributes.Add("type", "checkbox");
master_sub_login.Attributes.Add("checked", "checked");

master_staylogin_label.Controls.Add(master_sub_login);

master_actions.Controls.Add(master_staylogin_label);

master_login_content.Controls.Add(master_actions);

master_login.Controls.Add(master_login_content);


master_navigation_hori.Controls.Add(master_login);

And move these controls creation to Page_Init instead of Page_Load. Because page load the content-page first and then load the master-page.