开发者

Dynamically adding user control to page using place holder

I am attempting to add a user control to a place holder. I am placing the user control inside of a list item <li> userControl </li>, then placing the list item inside of an unordered list <ul><li>user control</li></ul>, and then adding the unordered list to the place holder, phMain.Controls.Add(myUnorderedList)... Code below:

var myListItem = new HtmlGenericControl("li");
var preview = new UserControls_ChiotsPreview();
preview = (UserControls_ChiotsPreview)LoadControl(typeof(UserControls_ChiotsPreview), null);
myListItem.Controls.Add(preview);
myUno开发者_开发百科rderedList.Controls.Add(myListItem);
placeholderMain.Controls.Add(myUnorderedList);


I figured it out, it was the order in which I was loading the control on the page and calling the constructor logic which caused my control to have null values.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜