未能控件动态添加到页面 [英] Failing to add controls to a page dynamically

查看:74
本文介绍了未能控件动态添加到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加一个用户控制每个记录在数据读取器拉升,这里的基本循环:

I'm adding a User Control for each record pulled up in a data reader, here's the basic loop:

while (dr.Read())
{
     ImageSelect imgSel = new ImageSelect(dr["Name"].ToString());
     myPanel.Controls.Add(imgSel);
}

问题是,有没有添加到页面控件,我检查HTML输出,并有我的面板,用什么也没有。

The problem is that there are no controls added to the page, I check the html output and there is my panel, with nothing in it.

我甚至通过在调试器中code踩,并验证myPanel.Controls被添加在每个循环的控制,与计数为6,没有错误,但他们不显示在页面上。

I even stepped through the code in the debugger, and verified that myPanel.Controls gets a control added on each loop, with the count being 6, no errors, but then they dont show up on the page.

我已经运行了超过​​code在Page_Init和Page_Load中的事件,都具有相同的结果。

I've run the above code in the Page_Init and Page_Load events, both with the same result.

编辑:
好了,所以我已经转向使用LoadControl(...... ASCX)来获取我的控件实例,这是现在的工作。但是,原本我也传递通过控制构造数据..难道这还是有可能的还是我只需要通过get /套进行设置?

Ok so I've switched to using LoadControl("....ascx") to get my control instance, which is now working. But originally I was also passing in data via the controls constructor.. Is this still possible or do I just need to set them via get/sets?

编辑2:
感谢弗雷迪的指出了LoadControl具有过载,你可以通过在构造函数参数,可以看到接受的答案。

EDIT 2: Thanks to Freddy for pointing out that the LoadControl has an overload where you CAN pass in constructor params, see accepted answer.

修改3:
既没有构造尝试此方法后。我发现它能够更好地只使用setter方法​​我想控制拥有与试图使用对象数组中通过了我的构造函数的任何属性。

EDIT 3: After trying this method both with and without the constructor. I have found its better to just use setters for any properties I want the control to have versus trying to use the passed in object array for my constructor.

推荐答案

更新:作为史蒂夫指出,LoadControl的使用类型的重载不会考虑在ASCX控件。这也是在这一答复中提到:<一href=\"http://stackoverflow.com/questions/450431/dynamically-loading-a-usercontrol-with-loadcontrol-method-type-object/450449#450449\">http://stackoverflow.com/questions/450431/dynamically-loading-a-usercontrol-with-loadcontrol-method-type-object/450449#450449.

Update: As Steve pointed out, the overload of LoadControl that uses the type won't take into account the controls in the ascx. This is also mentioned in this answer: http://stackoverflow.com/questions/450431/dynamically-loading-a-usercontrol-with-loadcontrol-method-type-object/450449#450449.

正如我前面提到的,get / set方法是比较符合asp.net示范线,所以我建议使用与接收用户控制路径的LoadControl变化。这就是说,史蒂夫的版本是一个有趣的选择:的http:/ /www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/

As I mentioned before, the get/set are more in line with the asp.net model, so I recommend using that with the LoadControl variation that receives the user control path. That said, the Steve's version is an interesting alternative: http://www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/.

我采取的是与类型LoadControl是为了与网络,而不是自定义控件使用。

My take is the LoadControl with type is meant to be used with web custom controls instead.


如果这是你应该使用LoadControl(usercontrolpath)来获取用户控件的实例的用户控件。

If it is an user control you should use LoadControl(usercontrolpath) to get the instance of the user control.

您可以通过执行使用构造函数:

You can use a constructor by doing:

var name = dr["Name"].ToString();
var imgSel = LoadControl(typeof(ImageSelect), new object[]{ name });
myPanel.Controls.Add(imgSel);

请注意,这取决于你所使用的项目模型,你需要添加一个引用到ASPX到使用typeof变化使用它:

Notice that depending on the project model you are using, you need to add a Reference to the aspx to use it with the typeof variation:

<%@ Reference Control="~/somepath/myusercontrol.ascx" %>

诗。我通常使用set /得到控制,因为我觉得他们更符合asp.net模型

Ps. I usually use the set/get for controls as I find them more in line with the asp.net model

这篇关于未能控件动态添加到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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