ASP.NET正在尝试查找我的ID是否是CheckBoxList控件的子控件之一
本文关键字:控件 CheckBoxList 是否是 ID 我的 NET 查找 ASP | 更新日期: 2023-09-27 18:30:04
我被这个问题卡住了,似乎找不到解决它的方法。我有一个CheckBoxList控件。如果您不知道,CheckBoxList控件上的FindControl()方法会返回"this"。微软之所以这么做,是因为他们在内部不创建很多ListItem对象,只创建了一个。无论如何,我正在努力找出一个张贴的控件是否是我的CheckBoxList中的控件之一。我的代码看起来像是:
if (!(System.Web.UI.ScriptManager.GetCurrent(Page) == null)) {
string postbackControlId = System.Web.UI.ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;
if (!string.IsNullOrEmpty(postbackControlId))
{
Control control = ControlFinder.RecursiveFindChildControl(Controls, postbackControlId);
if (!(control == null))
{ }
}
}
有没有枚举CheckBoxList的子控件,或者查找我的ID是否等于他们的ID?
谢谢,Mike
CheckBoxList
中CheckBox
的UniqueID
是CheckBoxList
的UniqueID
加上$
加上项目的索引,因此您可以检查postbackControlId
是否是CheckBox
控件之一,如下所示:
if (postbackControlId.StartsWith(this.checkBoxList.UniqueID + "$"))
{
int itemIndex = Convert.ToInt32(
postbackControlId.Substring(this.checkBoxList.UniqueID.Length + 1), 10);
// ...
}
如果您只想查明回发是否是由CheckBoxList
中的某个项引起的,则不需要遍历整个控件层次结构。你甚至不需要深入到列表中。像这样的东西应该很好用:
string elementID = ScriptManager.GetCurrent(Page).AsyncPostBackSourceElementID;
if (elementID.Contains(chkList.UniqueID))
{
//one of the checkboxes caused the postback
}