Reputation: 7053
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!!!!
Upvotes: 0
Views: 112
Reputation: 16018
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.
Upvotes: 1