假设我有一个 ASPxGridView,它使用几种不同类型的模板,并且每个模板上都有一个 ASPxButton。我还有一些不在 ASPxGridView 上的按钮。如果我递归遍历页面控件集合中的所有控件并更改找到的任何 ASPxButton 的按钮文本(或其他属性),则页面上的所有 ASPxButtons 都将更改,除了 ASPxGridView 上的那些。
为什么 ASPxGridView 上的 ASPxButtons 没有改变?ASPxGridView 模板上的按钮是否不在 ASPxGridViews 控件集合中?
似乎我将不得不对 ASPxGridViews 进行特殊处理才能在它们上找到按钮......我不能使用 FindTemplateControl 方法,因为我不知道 id。我怎样才能做到这一点?
提前致谢!
2013 年 3 月 7 日编辑:
这是我正在做的一个示例(这在母版页加载事件中调用,它适用于除 ASPxGridView 模板中的按钮之外的所有按钮......可能是其他模板):
/// <summary>
/// http://stackoverflow.com/questions/2632302/loop-through-all-the-user-controls-on-a-page
/// </summary>
/// <param name="controls">Page.Controls or some other control collection.</param>
private void setUseSubmitBehavior(ControlCollection controls)
{
foreach (Control ctrl in controls)
{
if (ctrl is ASPxButton)
{
if ((ctrl as ASPxButton).ID != GlobalProperties.SubmitButtonID)
(ctrl as ASPxButton).UseSubmitBehavior = false;
}
else if (ctrl is Button)
{
if ((ctrl as Button).ID != GlobalProperties.SubmitButtonID)
(ctrl as Button).UseSubmitBehavior = false;
}
try
{
if (ctrl.Controls.Count > 0)
setUseSubmitBehavior(ctrl.Controls);
}
catch (Exception ex)
{
//Don't let the page fail here... just add a log and let it fail on the page that caused the error.
MiscUtility.ExceptionLog(Page.GetType().AssemblyQualifiedName + " - " + MethodInfo.GetCurrentMethod().Name, ex.Message, "");
}
}
}
这是我第一次尝试在 GridViews 模板按钮上调用它的示例......但它不起作用:
private void setUseSubmitBehavior(ControlCollection controls)
{
foreach (Control ctrl in controls)
{
if (ctrl is ASPxButton)
{
if ((ctrl as ASPxButton).ID != GlobalProperties.SubmitButtonID)
(ctrl as ASPxButton).UseSubmitBehavior = false;
}
else if (ctrl is Button)
{
if ((ctrl as Button).ID != GlobalProperties.SubmitButtonID)
(ctrl as Button).UseSubmitBehavior = false;
}
else if (ctrl is DevExpress.Web.ASPxGridView.ASPxGridView)
{
setUseSubmitBehaviorOnASPxGridView((ctrl as ASPxGridView));
continue;//This one is handled differently.
}
try
{
if (ctrl.Controls.Count > 0)
setUseSubmitBehavior(ctrl.Controls);
}
catch (Exception ex)
{
//Don't let the page fail here... just add a log and let it fail on the page that caused the error.
MiscUtility.ExceptionLog(Page.GetType().AssemblyQualifiedName + " - " + MethodInfo.GetCurrentMethod().Name, ex.Message, "");
}
}
}
private void setUseSubmitBehaviorOnASPxGridView(ASPxGridView theGridView)
{
try
{
List<GridViewBaseTemplateContainer> listTemplateContainers = new List<GridViewBaseTemplateContainer>();
if (theGridView.Templates.DataItem != null)
listTemplateContainers.Add((theGridView.Templates.DataItem as GridViewDataItemTemplateContainer));
if (theGridView.Templates.DataRow != null)
listTemplateContainers.Add((theGridView.Templates.DataRow as GridViewDataRowTemplateContainer));
if (theGridView.Templates.DetailRow != null)
listTemplateContainers.Add((theGridView.Templates.DetailRow as GridViewDetailRowTemplateContainer));
if (theGridView.Templates.EditForm != null)
listTemplateContainers.Add((theGridView.Templates.EditForm as GridViewEditFormTemplateContainer));
if (theGridView.Templates.EmptyDataRow != null)
listTemplateContainers.Add((theGridView.Templates.EmptyDataRow as GridViewEmptyDataRowTemplateContainer));
if (theGridView.Templates.FooterCell != null)
listTemplateContainers.Add((theGridView.Templates.FooterCell as GridViewFooterCellTemplateContainer));
if (theGridView.Templates.FooterRow != null)
listTemplateContainers.Add((theGridView.Templates.FooterRow as GridViewFooterRowTemplateContainer));
if (theGridView.Templates.GroupRow != null)
listTemplateContainers.Add((theGridView.Templates.GroupRow as GridViewGroupRowTemplateContainer));
if (theGridView.Templates.GroupRowContent != null)
listTemplateContainers.Add((theGridView.Templates.GroupRowContent as GridViewBaseTemplateContainer));
if (theGridView.Templates.Header != null)
listTemplateContainers.Add((theGridView.Templates.Header as GridViewHeaderTemplateContainer));
if (theGridView.Templates.HeaderCaption != null)
listTemplateContainers.Add((theGridView.Templates.HeaderCaption as GridViewBaseTemplateContainer));
if (theGridView.Templates.PagerBar != null)
listTemplateContainers.Add((theGridView.Templates.PagerBar as GridViewPagerBarTemplateContainer));
if (theGridView.Templates.PreviewRow != null)
listTemplateContainers.Add((theGridView.Templates.PreviewRow as GridViewPreviewRowTemplateContainer));
if (theGridView.Templates.StatusBar != null)
listTemplateContainers.Add((theGridView.Templates.StatusBar as GridViewStatusBarTemplateContainer));
if (theGridView.Templates.TitlePanel != null)
listTemplateContainers.Add((theGridView.Templates.TitlePanel as GridViewBaseTemplateContainer));
foreach (GridViewBaseTemplateContainer container in listTemplateContainers)
{
//The problem with this and other attempts is that container is always null even if listTemplateContainers is populated.
if (container != null)
{
ControlCollection cc = container.Controls;
if (cc != null && cc.Count > 0)
setUseSubmitBehavior(cc);
}
}
}
catch (Exception ex)
{
//Don't let the page fail here... just add a log and let it fail on the page that caused the error.
MiscUtility.ExceptionLog(Page.GetType().AssemblyQualifiedName + " - " + MethodInfo.GetCurrentMethod().Name, ex.Message, "");
}
}
附带说明.... 我认为第一种方法缺少 ASPxGridView 模板上的按钮的原因是它们的 InstantiateIn 函数是在母版页加载事件之后调用的。我还尝试了母版页 preRender 事件,但之后仍会调用 InstantiateIn。