使用FindControl找不到我的控制 [英] Can´t find my Control with FindControl

查看:148
本文介绍了使用FindControl找不到我的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上工作,有问题。
我从主页面添加控件动态到contentplaceholder。
然后当我想访问的项目我找不到他们的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: / p>

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.

主页后面的代码:

 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, MEssageBox。
唯一找到的是master_navigation_hori

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

注意

推荐答案

我已经测试过您的代码。您必须使用 System.Web.UI.HtmlControls.HtmlGenericControl ,而不是 LiteralControl

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

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

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.

这篇关于使用FindControl找不到我的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆