我正在尝试将用户控件添加到占位符。我将用户控件放在列表项<li>
userControl </li>
中,然后将列表项放在无序列表<ul><li>
用户控件</li></ul>
中,然后添加无序列表到占位符,phMain.Controls.Add(myUnorderedList)...代码如下:
var myListItem = new HtmlGenericControl("li");
var preview = new UserControls_ChiotsPreview();
preview = (UserControls_ChiotsPreview)LoadControl(typeof(UserControls_ChiotsPreview), null);
myListItem.Controls.Add(preview);
myUnorderedList.Controls.Add(myListItem);
placeholderMain.Controls.Add(myUnorderedList);
答案 0 :(得分:0)
我弄明白了,这是我在页面上加载控件并调用构造函数逻辑的顺序,这导致我的控件具有空值。
var myUnorderedList = new HtmlGenericControl("ul");
myUnorderedList.Attributes["class"] = "resultClass";
var counter = 0;
foreach (var chiot in chiots)
{
//create list item, can I put the user control inside of here? why not?
var myListItem = new HtmlGenericControl("li");
//add user control to li
var preview = (UserControls_ChiotsPreview)LoadControl("~/UserControls/ChiotsPreview.ascx");
preview.Name = chiot.Name;
preview.MainPhoto = chiot.MainImage;
preview.Breed = chiot.Breed;
preview.ChampionSireDam = ResolveImage(chiot.ChampionSireDam);
preview.ChampionBloodline = ResolveImage(chiot.ChampionBloodline);
preview.Price = chiot.Price;
preview.Details = chiot.Description;
myListItem.Controls.Add(preview);
//add li to ul
myUnorderedList.Controls.Add(myListItem);
//max column = 5
counter += 1;
//maximum 5 columns
if (counter == 5 || chiots.Count == counter)
{
try
{
phMain.Controls.Add(myUnorderedList);