在不使用面板的情况下动态访问动态创建的控件 [英] Accessing Dynamically Created Controls Dynamically Without Using A Panel

查看:89
本文介绍了在不使用面板的情况下动态访问动态创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试创建一个简单的解决方案,以解决需要动态添加GridViews的问题.

我将网格的已知代码放入StringBuilder对象,并将其值传递给div的InnerHtml.我不使用面板,因为我不想花更多的时间测试实用创建的网格.

问题是我无法从后面的代码访问控件.我一直试图通过findcontrol和递归findcontrol来做到这一点,但无济于事.

帮助.

**
嗨Nelek.希望对您有所帮助.

我没有列出网格定义.

 StringBuilder sbGridHtml = new StringBuilder();
sbGridHtml.Append(" <     Count;     i ++) 
 
  { 
 
          sbGridHtml.Append(< tr   td   div   asp:GridView   ID   =   \"  gvSubject"+   i.ToString()    +"\"     =   服务器\" / > );
    sbGridHtml.Append("/div  /td  /tr  /table  <     Count;     i ++) 
 
  { 
 
  GridView     gv     =  (GridView)Page.FindControlRecursively("  gv"  +   i.ToString());  
  .......  
 
 }  



**
你好.

@SA对不起,我不确定我是否理解您的意思.但是,我已在代码中将自定义控件切换为ASP控件.
@GregWyatt:双方都同意.会在表格的每一行中排列一个,并且它们将具有可操作的按钮.

解决方案

我不能那样工作. .aspx页面不是包含字符串内容的文件.稍后将其编译为复杂的类,然后使用Response.write吐出内容.
如果必须添加动态控件,则必须通过ControlColection属性将其作为对象添加到其他控件.
如果要使用div进行此操作,请添加到此div控件集合中.

如果您不想创建对象

new AspxGridView(); 


在标记中执行此操作:

 <% 如果(CreateGrids)%>
 <   asp:gridview    运行  ="   xmlns:asp    #unknown" <  /asp:gridview  >  


Hi
I''m trying to created a simple solution to a problem that requires GridViews to be added dynamically.

I put known code for the grid into a StringBuilder object and passed its value to the InnerHtml of a div. I''m not using a Panel because the I don''t want to spend more time testing a pragmatically created grid.

The problem is that I''m not able to access the control from the code behind. I have been trying to do this via findcontrol and the recursive findcontrol but to no avail.

Help.

**
Hi Nelek. I hope this helps.

I have left out grid definition.

StringBuilder sbGridHtml = new StringBuilder();
sbGridHtml.Append("<table>");

for (int i = 0; i < Count; i++)

{

    sbGridHtml.Append("<tr>");
    sbGridHtml.Append("<td>");
    sbGridHtml.Append("<div>");
    sbGridHtml.Append("<asp:GridView ID=\"gvSubject"+ i.ToString() +"\" runat=\"server\" />");
    sbGridHtml.Append("</div>");
    sbGridHtml.Append("</td>");
    sbGridHtml.Append("</tr>");
    #endregion Grid HTML

}
sbGridHtml.Append("</table>");
DivGrid.InnerHtml = sbGridHtml.ToString();

I later try to access the grid using the below code. 
for (int i = 0; i < Count; i++)

{

GridView gv = (GridView)Page.FindControlRecursively("gv" + i.ToString());

.......

}



**
Hi.

@SA I''m sorry, I''m not sure I understand what you are saying. I have however switched custom controls to asp controls in the code.
@GregWyatt: Yes to both. The will be arranged one per row of the table and they will have actionable buttons.

解决方案

I does not work that way. .aspx page is not a file with string contents. It is later compiled to complex class witch later spit it''s contents with Response.write.
If you have to add dynamic controls you have to add it as objects to some oth

er control via ControlColection property.
If you want do this with div, add to this div control collection.

If you don''t want do this with creation of objects

new AspxGridView(); 


Do this in markup:

 <%if(CreateGrids) %> 
 <asp:gridview runat="server" xmlns:asp="#unknown">
</asp:gridview>


这篇关于在不使用面板的情况下动态访问动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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