以下代码在异步回调期间不更新UpdatePanel的内容:
protected void getMoreInfoCommand_Click(object sender, CommandEventArgs e)
{
Button button = (Button)sender;
UpdatePanel updatePanel = (UpdatePanel)button.Parent.Parent;
updatePanel.ContentTemplateContainer.Controls.Clear();
switch(e.CommandName)
{
case "PERSON":
Person.Portal.UserProfile userProfile = new Person.Portal.UserProfile(e.CommandArgument.ToString());
updatePanel = getPanel(userProfile, true);
break;
}
}
protected UpdatePanel getPanel(object obj, bool more)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
updatePanel.EnableViewState = true;
updatePanel.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
updatePanel.ChildrenAsTriggers = true;
StringBuilder htmlBuilder = new StringBuilder();
switch (obj.GetType().ToString().Trim())
{
case "Person.Portal.UserProfile":
Person.Portal.UserProfile userProfile = (Person.Portal.UserProfile)obj;
htmlBuilder.Append("<TABLE border='1' cellpadding=3 cellspacing=0 width=20%>");
htmlBuilder.Append("<tr><td colspan=2>");
htmlBuilder.Append("<font color='gray' size=2>Person</font>");
updatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl(htmlBuilder.ToString()));
htmlBuilder.Length = 0;
Button morePersonButton = new Button();
morePersonButton.ID = "morePersonButton";
morePersonButton.Text = "More";
morePersonButton.CommandName = "PERSON";
morePersonButton.CommandArgument = userProfile.ID;
morePersonButton.Command += new CommandEventHandler(getMoreInfoCommand_Click);
updatePanel.ContentTemplateContainer.Controls.Add(morePersonButton);
htmlBuilder.Append("<tr><td align='center' colspan=2>");
htmlBuilder.Append("<img src='https://webadvisor.oc.edu/pictures/");
htmlBuilder.Append(userProfile.Pic_ID);
htmlBuilder.Append(".jpg'>");
htmlBuilder.Append("</tr>");
htmlBuilder.Append("<tr><td>");
htmlBuilder.Append("<B>Name</B><TD>");
htmlBuilder.Append(userProfile.FirstName + " " + userProfile.LastName);
htmlBuilder.Append("</tr>");
htmlBuilder.Append("<tr><td>");
htmlBuilder.Append("<B>Email Address</B><TD>");
htmlBuilder.Append(userProfile.EmailAddress);
htmlBuilder.Append("</tr>");
htmlBuilder.Append("<tr><td>");
htmlBuilder.Append("<B>Phone</B><TD>");
htmlBuilder.Append(userProfile.Phone);
htmlBuilder.Append("</tr>");
if (more)
{
if (!String.IsNullOrEmpty(userProfile.Office_Number))
{
htmlBuilder.Append("<tr><td>");
htmlBuilder.Append("<B>Office Building</B><TD>");
htmlBuilder.Append(userProfile.Office_Number);
htmlBuilder.Append("</tr>");
}
}
htmlBuilder.Append("</TABLE><BR>");
updatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl(htmlBuilder.ToString()));
htmlBuilder.Length = 0;
break;
}
return updatePanel;
}
但是,如果我执行以下操作,则可以:
protected void getMoreInfoCommand_Click(object sender, CommandEventArgs e)
{
Button button = (Button)sender;
UpdatePanel updatePanel = (UpdatePanel)button.Parent.Parent;
updatePanel.ContentTemplateContainer.Controls.Clear();
switch(e.CommandName)
{
case "PERSON":
Person.Portal.UserProfile userProfile = new Person.Portal.UserProfile(e.CommandArgument.ToString());
getPanel(userProfile, true, ref updatePanel);
break;
}
}
protected void getPanel(object obj, bool more, ref UpdatePanel updatePanel)
{
...
}
我在别处使用getPanel(),并不想改变它的工作方式。我不明白为什么第一个例子不起作用。我错过了什么?有解决方法吗?
答案 0 :(得分:0)
更新面板已经渲染,您必须从其父控件集合中替换更新面板。更改“更新面板”父级的控件集合将正确更新它。