通过嵌套的 ajax 手风琴 Web 控件向上遍历;类型检查和 FindControl
我有嵌套的 ajax 手风琴 Web 控件(嵌套 3 层或有时 4 层),我正在尝试找到嵌套的手风琴的父手风琴。我想在最深层的手风琴中的任何一个成为数据绑定时执行此操作。
protected void grandChildAccordion_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
{
if (e.AccordionItem.ItemType == AjaxControlToolkit.AccordionItemType.Header)
{
// find parent accordion
}
}
有时,父折叠面板将处于 e.AccordionItem.Parent.Parent.Parent 级别或 e.AccordionItem.Parent.Parent.Parent.Parent 级别。
那么,如何检查 e.AccordionItem.Parent
是否等于 AjaxControlToolkit.AccordionContentPanel
“type”?
(不太优雅)解决方案:我有嵌套的手风琴,最深的手风琴级别包含文档列表,每个手风琴标题都有一个显示计数的标签嵌套在其中的文档。从最深的手风琴级别开始,我获得文档计数,我遍历嵌套的手风琴并找到每个手风琴标题,使我能够找到他们的文档计数标签并为其添加一个值..希望这对某人有帮助:
// Save doc count. It will be displayed in doc count label within current accordion header, and the value will then be added to the doc count of each parent accordion's header.
int curDocCount = qcDocsBO.FetchQCDocumentCountByProjectID(SessionHandler.ProjectID, qcDocsBO.LSDItemID);
lblDocCount.Text = curDocCount.ToString();
// A deep-most accordion has just been DataBound. Start traversing upwards through nested accordions, adding the doc count to each parent accordion header's doc count label.
AjaxControlToolkit.Accordion curAccordion = sender as AjaxControlToolkit.Accordion;
AjaxControlToolkit.AccordionPane parentAccCP = curAccordion.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (parentAccCP == null)
parentAccCP = curAccordion.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
while (parentAccCP != null)
{
Label parentDocCountLabel = (Label)parentAccCP.FindControl("lblDocCount");
if (parentDocCountLabel.Text == "")
parentDocCountLabel.Text = "0";
parentDocCountLabel.Text = (Convert.ToInt32(parentDocCountLabel.Text) + curDocCount).ToString();
AjaxControlToolkit.AccordionPane nextParentAccCP = parentAccCP.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP != null)
parentAccCP = nextParentAccCP;
else
parentAccCP = null;
}
I have nested ajax accordion web controls (nested 3 deep or sometimes 4 deep) and I am trying to find a nested accordion's parent accordion. I want to do this when either of the deep-most accordions becomes databound.
protected void grandChildAccordion_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
{
if (e.AccordionItem.ItemType == AjaxControlToolkit.AccordionItemType.Header)
{
// find parent accordion
}
}
Sometimes the parent accordion will be at e.AccordionItem.Parent.Parent.Parent level or e.AccordionItem.Parent.Parent.Parent.Parent level.
So, how do I check if e.AccordionItem.Parent
equals AjaxControlToolkit.AccordionContentPanel
"type"?
The (not-so-elegant) Solution: I have nested accordions, the deep-most accordion levels contains lists of documents, and each accordion header has a label showing a count of documents nested within it. Starting from the deep-most accordion level where I get a count of documents, I traverse up the nested accordions and find each accordion header, allowing me to find their doc count label and add a value to it.. hope this helps someone:
// Save doc count. It will be displayed in doc count label within current accordion header, and the value will then be added to the doc count of each parent accordion's header.
int curDocCount = qcDocsBO.FetchQCDocumentCountByProjectID(SessionHandler.ProjectID, qcDocsBO.LSDItemID);
lblDocCount.Text = curDocCount.ToString();
// A deep-most accordion has just been DataBound. Start traversing upwards through nested accordions, adding the doc count to each parent accordion header's doc count label.
AjaxControlToolkit.Accordion curAccordion = sender as AjaxControlToolkit.Accordion;
AjaxControlToolkit.AccordionPane parentAccCP = curAccordion.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (parentAccCP == null)
parentAccCP = curAccordion.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
while (parentAccCP != null)
{
Label parentDocCountLabel = (Label)parentAccCP.FindControl("lblDocCount");
if (parentDocCountLabel.Text == "")
parentDocCountLabel.Text = "0";
parentDocCountLabel.Text = (Convert.ToInt32(parentDocCountLabel.Text) + curDocCount).ToString();
AjaxControlToolkit.AccordionPane nextParentAccCP = parentAccCP.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP == null)
nextParentAccCP = parentAccCP.Parent.Parent.Parent.Parent.Parent.Parent as AjaxControlToolkit.AccordionPane;
if (nextParentAccCP != null)
parentAccCP = nextParentAccCP;
else
parentAccCP = null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您始终可以使用
is
关键字进行类型检查,或使用as
关键字进行安全转换。请考虑以下情况:
则此
如果安全转换不成功,
accordion
为 null。使用这两种技术中的一种或两种,您可以在层次结构中向上迭代,并在遇到要查找的类型时停止。You can always do type checks using the
is
keyword or safe cast using theas
keyword.Consider the following:
or this
accordion
is null if the safe cast wasn't successful.Using either or both of these techniques, you can iterate upwards in the hierarchy and stop when you encounter the type you are looking for.