简体   繁体   English

查找控件无法找到使用文字控件创建的控件

[英]find control not able to find controls that created with literal control

this is MyFirstRow in my .aspx file: 这是我的.aspx文件中的MyFirstRow:

<ul class="dropdown-menu" id="MyFirstRow" runat="server">

I created the control with this Code in CodeBehind : 我在CodeBehind中使用以下代码创建了控件:

Control Con = FindControl("MyFirstRow");
LiteralControl LCx = new LiteralControl();
LCx.Text = @"<li class='dropdown' id='MyFirstRow34' runat='server'>
<a href='#'>Sample<span class='caret'></span></a></li>";
Con.Controls.Add(LCx);

so far everything is ok, but after next line when I use this code, this not able to find any control In the event that this added Correctly: 到目前为止,一切正常,但是在下一行之后,当我使用此代码时,这找不到任何控件。如果正确添加了此控件:

Con = FindControl("MyFirstRow34");

where is my mistake? 我的错误在哪里? tnx for your response. 谢谢您的回应。

Edited : My question is how to access this control with id=MyFirstRow34 in Code-Behind? 编辑:我的问题是如何在Code-Behind中使用id = MyFirstRow34访问此控件?

The <li class='dropdown' id='MyFirstRow34' runat='server'> string content assigned to the LiteralControl doesn't get interpreted by ASP.NET ; 分配给LiteralControl<li class='dropdown' id='MyFirstRow34' runat='server'>字符串内容不会被ASP.NET解释; this string is only part of the html output. 此字符串只是html输出的一部分。

In order to find the control by the given Id, you must assign the Id to the controls ID property, as shown below. 为了通过给定的ID查找控件,必须将ID分配给控件ID属性,如下所示。

LiteralControl LCx = new LiteralControl { ID = "MyFirstRow34" };

The full code of your example will look like this: 您的示例的完整代码如下所示:

Control Con = FindControl("MyFirstRow");
LiteralControl LCx = new LiteralControl { ID = "MyFirstRow34" };
LCx.Text = @"
    <li class='dropdown'>
        <a href='#'>Sample<span class='caret'></span></a>
     </li>";
Con.Controls.Add(LCx);

Control myFirstRow34 = FindControl("MyFirstRow34");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM