这个例子看起来有点长。但是有必要理解我的问题。
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;
}
}
其他信息:
FirstLoad只是一个道具:
public bool FirstList { get { return ViewState["first"] == null ? false : (bool)ViewState["first"]; } set { ViewState["first"] = value; } }
ListFirstPage()方法如下所示:
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();
}
}
按钮事件如下所示:
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();
}
}
}
这会发生什么..当我单击表单上的按钮时,会触发ListFirstPage(),这会导致列出的表列表和页面栏(带有数字的2个按钮,即1 ,2)。当我按下按钮2时,我希望按钮事件内的迭代发生/但相反,没有任何反应,表单变为空白并且没有生成任何内容。这是为什么?请注意,ListFirstPage中的算法和按钮事件是相同的!!!!
答案 0 :(得分:1)
不要忘记你必须re-create all dynamic controls on postback
你的Page
只是一个记住的类,并且每个请求都会实例化一次,如果它没有重新创建这些控件以及回发请求上的关联处理程序,那么你就不会发生任何事情。
您需要在Page_Load
之前重新创建这些控件,您可以在Page_Init
中执行此操作或覆盖CreateChildControls
方法。