0

The example looks a bit long. But it is necessary to understand my question.

if (IsPostBack)

    {

            for (int j = 0; j < PostsDic.Count; j++)//The number is 2. 2 buttons to be created.
            {
                Button pgs2 = new Button();//Create New Topic
                pgs2.Width = 20;
                pgs2.Command += obtainTopicsPerPage_Click;
                pgs2.EnableViewState = false;
                pgs2.CommandName = j.ToString();
                pgs2.Text = j.ToString();
                buttons.Add(pgs2);

            }
            if (!FirstList)
            {
                ListFirstPage();//Creates a few tables and makes it look like a thread table in a forum
                FirstList = true;
            }



    }

Additional information:

FirstLoad is simply a prop:

public bool FirstList { get { return ViewState["first"] == null ? false : (bool)ViewState["first"]; } set { ViewState["first"] = value; } }

ListFirstPage() method looks like this:

    void ListFirstPage()
{
    //Dictionary<int, List<AllQuestionsPresented>>
    foreach (var item in PostsDic)
    {

       foreach (var apply in PostsDic[item.Key])
       {
          DisplayAllQuestionsTable objectToList = new DisplayAllQuestionsTable(this, apply.Name, apply.ThreadName, apply.Topic, apply.Subtopic, apply.Views, apply.Replies, apply.PageNumber, apply.Time, PlaceHolder2);
          objectToList.ExecuteAll();
       }

    }

The button event looks like this:

enter code here   void obtainTopicsPerPage_Click(Object sender, CommandEventArgs e)
{
    //Dictionary<int, List<AllQuestionsPresented>>
    foreach (var item in PostsDic)
    {
        if (item.Key.ToString() == e.CommandName)
        {
            int ds=0;

            foreach (var apply in PostsDic[item.Key])
            {
                DisplayAllQuestionsTable objectToList = new DisplayAllQuestionsTable(this, apply.Name, apply.ThreadName, apply.Topic, apply.Subtopic, apply.Views, apply.Replies, apply.PageNumber, apply.Time, PlaceHolder2);
                objectToList.ExecuteAll();

            }
        }
    }

What happens is this.. When I click a button that i have on the form the ListFirstPage() is triggered, this leads to a list of tables being listed, and a page bar (2 buttons with numbers on them i.e. 1 ,2). When i press button 2 i expect the iteration inside the button event to happen/ But instead, nothing happens the form goes blank and nothing is generated. Why is that? Note that the algorithm in the ListFirstPage and the buttons events is identical!!!!

asawyer
  • 17,642
  • 8
  • 59
  • 87
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160
  • Have you stepped through the code? Have you verified that "Nothing happens"? What is PostsDic (where is it generated? Is it available on the postback?)... And the algorithm in ListFirstPage and button events are not identical, there are additional conditional statments in the button events. – Colin Mackay Jun 20 '11 at 13:23
  • Yeah, i did, everything is fired.. After the pageLoad event no button events are fired. But i solved it. It appears that all the controls need to be added at Page Load. if they are place onto the page a bit later, no button control would fire – Dmitry Makovetskiyd Jun 20 '11 at 13:42

1 Answers1

1

Don't forget you must re-create all dynamic controls on postback

Your Page is just a class remember and it is instantiated once per request, if it doesn't recreate these controls as well as the associated handlers on the postback request then you won't get anything happen..

You need to recreate these controls prior to Page_Load, you can do this in Page_Init or override the CreateChildControls method.

Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • I found the mistake, just after i recreated the page on the Load page event, i had to place all the buttons inside a place holder. The reason it worked then, was that if the controls arent on the page in that stage, their events wont be fired later on.. Since i put the buttons on the page later on in the code, they didnt have the time to fire..and this resulted in blank page///cause no button events were fired. Am I right? in the reason? – Dmitry Makovetskiyd Jun 20 '11 at 13:40