假设我有一个ASPxGridView,它使用了几种不同类型的模板,每个模板都有一个ASPxButton。我也有一些不在ASPxGridView上的按钮。如果我递归遍历页面控件集合中的所有控件并更改找到的任何ASPxButton的按钮文本(或其他属性),那么除了ASPxGridView上的ASPxButton之外,页面上的所有ASPxButtons都将被更改。
为什么ASPxGridView上的ASPxButtons没有改变? ASPxGridView模板上的按钮不在ASPxGridViews控件集合中吗?
似乎我将需要特殊情况ASPxGridViews来查找它们上的按钮.... 我不能使用FindTemplateControl方法因为我不知道id的。我怎么能这样做?
提前致谢!
编辑7Mar2013:
以下是我正在做的事情的示例(这在主页面Load事件中调用,它适用于除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。