Findcontrol返回null甚至存在ID

时间:2016-11-06 18:01:32

标签: c# html css asp.net web

我在我的网站上工作并遇到问题。 我将控件动态添加到母版页的contentplaceholder中。 然后,当我想访问项目时,我无法找到它们的Findcontrol方法,但控件在那里,因为我可以看到它们。

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> 

不知怎的,身体的结束标签不显示,但无论如何。

母版页背后的代码:

 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>"));

        }

    }

内容页码背后:

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");

    }

这将返回null,它只显示&#34; end&#34;消息框。 它找到的唯一的东西是master_navigation_hori

此致

2 个答案:

答案 0 :(得分:0)

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

    }

以上代码可帮助您使其正常工作。如果有效,请投票。

答案 1 :(得分:0)

我已经在测试你的代码了。您必须使用System.Web.UI.HtmlControls.HtmlGenericControl代替LiteralControl

所以,我做了这样的改变

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);

将这些控件创建移至Page_Init而不是Page_Load。因为页面首先加载内容页面,然后加载主页面。