在我的 UserControl 中,我尝试像这样更新中继器内的更新面板:
HTML 标记
<asp:UpdatePanel ID="updDocumentQuickView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="repFolders" runat="server" OnItemDataBound="repFolders_OnItemDataBound" OnItemCommand="repFolders_OnItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkFolder" runat="server"></asp:LinkButton>
<asp:UpdatePanel ID="updFiles" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="repFiles" runat="server" OnItemDataBound="repFiles_OnItemDataBound">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
C#代码
protected void repFolders_OnItemCommand(object sender, CommandEventArgs e)
{
int intRow = -1;
ScriptManager myScriptManager = (ScriptManager)Page.Master.FindControl("myScriptManager");
Match myMatch = Regex.Match(myScriptManager.AsyncPostBackSourceElementID, "repFolders.ctl([0-9]*).lnkFolder");
if (myMatch != null)
intRow = Convert.ToInt32(myMatch.Groups[1].Value);
if (intRow > -1)
{
RepeaterItem myItem = repFolders.Items[intRow];
Repeater repFiles = (Repeater)myItem.FindControl("repFiles");
UpdatePanel updFiles = (UpdatePanel)myItem.FindControl("updFiles");
string[] arr1 = new string[] {
"array item 1",
"array item 2",
"array item 3",
"array item 4",
"array item 5" };
repFiles.DataSource = arr1;
repFiles.DataBind();
updFiles.Update();
}
}
我得到的最终结果是 updDocumentQuickView 是更新的 UpdatePanel,而不是 updFiles。如果我将 UpdatePanel 包裹在 lnkFolder 周围,则该 UpdatePanel 会更新,使用相同的 C# 代码。我检查了用 fiddler 发回的数据类型,并发送了错误的 UpdatePanel。我得到了正确的 RepeaterItem,并且找到了 repFiles 和 updFiles。我错过了什么才能获得正确的 UpdatePanel 以进行更新?
更新
Hawxby 解决方案解决了 updDocumentQuickView 更新的问题,谢谢。但是我仍然遇到 updFiles 没有返回任何内容的问题。一些进一步的测试,将文字放入 updFiles 并开始工作,告诉我 repFiles 中有些东西没有返回。 repFiles 确实有有界的数据。
最终解决方案
repFiles.Visible在repFolders_OnItemDataBound中设置为false,难怪没有显示。
最佳答案
这可能是因为您必须显式设置异步绑定(bind)
<asp:UpdatePanel ID="updDocumentQuickView" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="repFolders" EventName="repFolders_OnItemCommand" />
</Triggers>
</asp:UpdatePanel>
关于c# - Repeater 中的 UpdatePanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5458585/