![](/img/trans.png)
[英]Unable to Find the Control from codebehind, but able to find other controls
[英]find control not able to find controls that created with literal control
这是我的.aspx文件中的MyFirstRow:
<ul class="dropdown-menu" id="MyFirstRow" runat="server">
我在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);
到目前为止,一切正常,但是在下一行之后,当我使用此代码时,这找不到任何控件。如果正确添加了此控件:
Con = FindControl("MyFirstRow34");
我的错误在哪里? 谢谢您的回应。
编辑:我的问题是如何在Code-Behind中使用id = MyFirstRow34访问此控件?
分配给LiteralControl
的<li class='dropdown' id='MyFirstRow34' runat='server'>
字符串内容不会被ASP.NET
解释; 此字符串只是html输出的一部分。
为了通过给定的ID查找控件,必须将ID分配给控件ID
属性,如下所示。
LiteralControl LCx = new LiteralControl { ID = "MyFirstRow34" };
您的示例的完整代码如下所示:
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.