我正在尝试添加一个用户控件到占位符。我将用户控件放入列表项<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);
我弄清楚了,这是我在页面上加载控件并调用构造函数逻辑的顺序,这导致我的控件具有空值。
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);