![](/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.