Problem with dynamicly added buttons in PlaceHolder

Discussion in 'ASP .Net Web Controls' started by dotNet, Feb 15, 2005.

  1. dotNet

    dotNet Guest

    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?
     
    dotNet, Feb 15, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henke
    Replies:
    5
    Views:
    674
    Munsifali Rashid
    Dec 30, 2003
  2. Henke
    Replies:
    1
    Views:
    488
  3. Anders K. Jacobsen [DK]

    Viewstate and dynamicly added controls

    Anders K. Jacobsen [DK], Mar 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    436
    Karl Seguin
    Mar 22, 2005
  4. MikeM

    Dynamicly loaded User Control events in a Placeholder

    MikeM, Apr 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    221
    Jonathan Roberts
    Apr 25, 2004
  5. Mike Housey

    Persistance of Dynamicly added user controls.

    Mike Housey, Aug 5, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    133
    Ken Cox [Microsoft MVP]
    Aug 8, 2003
Loading...

Share This Page