我在 Page_Init 上添加了一组图像,并在某个按钮上单击我想更新这些图像。由于某种原因,它不起作用。为什么?
这是我的代码:
protected void Page_Init(object sender, EventArgs e)
{
...
for (int i = 0; i < questionsCount; i++)
{
TableRow newTableRow = new TableRow();
TableTestAnswers.Rows.Add(newTableRow);
TableCell questionNumberCell = new TableCell();
newTableRow.Cells.Add(questionNumberCell);
LinkButton questionNumberLinkButton = new LinkButton();
questionNumberCell.Controls.Add(questionNumberLinkButton);
questionNumberLinkButton.Text = ((char)(i + 'A')).ToString();
TableCell answersCell = new TableCell();
newTableRow.Cells.Add(answersCell);
answersCell.ID = "AnswersCell-" + i.ToString();
for (int j = 0; j < answersCount[i]; j++)
{
Image answerImage = new Image();
answersCell.Controls.Add(answerImage);
answerImage.ID = "Answer-" + i.ToString() + "-" + j.ToString();
answerImage.ImageUrl = Resources.Images.AnswerEmptySquareUrl;
}
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
...
Image answerImage = (Image)TableTestAnswers.FindControl(
"Answer-" + questionIndex.ToString() + "-" + answerIndex.ToString());
answerImage.ImageUrl = Resources.Images.AnswerMarkedUrl;
answerImage.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
}
我可以看到 ButtonSubmit 找到了正确的控件并更改了它的属性,但是在对客户端的响应中,我仍然看到旧图像。帮助!