Problem with dynamicly added buttons in PlaceHolder


D

dotNet

Hello!

I have created a custom repeater with a custom template.

In the custom template I can place custom controls that are for my
repeater. I have made custom buttons and a custom placeholder.

I add the buttons and placeholder to my template and everything works
alright. I add clickevents to my buttons and that works aswell, but when
I dynamicly add buttons to my placeholder and from them try to add a
clickevent it wont raise that event on the postback.

That is probably since the buttons in the placeholder is not created on
the postback, so that is kind of the source of the problem. But Im
stuck, and dont know how to move on. I post some code, and call for help :)


public class customRepeater : Repeater{

private PagedDataSource pds;

public PagedRepeater() : base()
{
pds = new PagedDataSource();
}

public override object DataSource
{
get {return base.DataSource;}
set
{
pds.DataSource = (IEnumerable) value;
base.DataSource = pds;
}
}

public override void CreateControlHierarchy(bool useDataSource)
{
this.Controls.Clear();

PagedRepeaterTemplateItem item = new PagedRepeaterTemplateItem();
PagingTemplate.InstantiateIn(item);

PagedRepeaterTemplateButton first = (PagedRepeaterTemplateButton)
item.FindControl("btnFirst");
first.Click += new EventHandler(First_Click);

PagedRepeaterTemplatePages pagesHolder =
(PagedRepeaterTemplatePages) item.FindControl("plcPages");
FillPlaceHolderWithPages(pagesHolder);

this.Controls.Add(item);

base.CreateControlHierarchy (useDataSource);
}

private void FillPlaceHolderWithPages(PagedRepeaterTemplatePages plcH)
{
plcH.Controls.Clear()

for (int i=pds.CurrentPageIndex; i<=pds.PageCount;i++)
{
LinkButton lb = new LinkButton();
lb.Text = i.ToString();
lb.CommandArgument = i.ToString();
lb.Click += new EventHandler(Page_Click);

plcH.Controls.Add(lb);
}
}
}


The First_Click event is triggered as it should, but the Page_Click is
not. That is as I said before because the pds.PageCount is 0 after a
postback.

I am thinking of storing the PageCount in the viewstate to maintain,
that could work, but it doesnt seem like the best solution. Any ideas?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top