这个例子看起来有点长。但有必要理解我的问题。
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()就会被触发,这会导致列出一个表格列表和一个页面栏(两个按钮上有数字,即1 ,2)。当我按下按钮2时,我期望按钮事件内部的迭代会发生/但是,没有发生任何事情,表单变为空白,并且没有生成任何内容。为什么会这样呢?请注意,ListFirstPage和buttons事件中的算法是相同的!
发布于 2011-06-20 21:34:51
别忘了你必须使用re-create all dynamic controls on postback
您的Page只是一个类,记住,它在每个请求中实例化一次,如果它不在回发请求上重新创建这些控件以及关联的处理程序,那么您将不会发生任何事情。
您需要在Page_Load之前重新创建这些控件,您可以在Page_Init中执行此操作或覆盖CreateChildControls方法。
https://stackoverflow.com/questions/6411153
复制相似问题