0

Lets say I have an ASPxGridView that uses several different types of templates and each of those templates have an ASPxButton on it. I also have some buttons that are not on the ASPxGridView. If I recursively loop through all controls in the pages controls collection and change the button text ( or other property ) of any ASPxButton that is found then all of the ASPxButtons on the page will be changed except for the ones on the ASPxGridView.

Why are the ASPxButtons on the ASPxGridView not changed? Are the buttons on the ASPxGridView templates not in the ASPxGridViews controls collection?

It seems like I will have to special case ASPxGridViews to find buttons on them.... I can't use the FindTemplateControl methods because I will not know the id's. How can I do this?

Thanks in advance!

Edit 7Mar2013:

Here is an example of what I am doing (this is called in the master page Load event and it works for all buttons except those in ASPxGridView templates... possibly other templates):

    /// <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, "");
            }
        }
    }

And here is an example of my first attempt at calling this on a GridViews templated buttons... But it did not work:

 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, "");
        }
    }

On a side note.... I think the reason the first method is missing buttons on ASPxGridView templates is that their InstantiateIn functions are called after the master page load event. I also tried the master page preRender event but InstantiateIn is still called after that.

Soenhay
  • 3,958
  • 5
  • 34
  • 60
  • You should explain why are you doing this. It seems to me that it should be better way to achieve your goals than iterating through all controls in page. – Filip Mar 07 '13 at 08:35
  • The better way would be to make sure everyone always sets UseSubmitBehavior to false unless they actually want the button to submit... I added some code examples that should explain what I am doing. – Soenhay Mar 07 '13 at 15:42
  • You could create custom control with button in it and set submit behaviour inside that control. – Filip Mar 08 '13 at 11:37
  • I like that Idea but then I have to convert all existing buttons to that control and make sure everyone uses the new control. – Soenhay Apr 23 '13 at 21:03
  • That's true. But you can have additional benefits from that in the future. E.g. what if you have to show/hide your buttons based on some authorization settings? It is much easier to achieve that if you wrap your buttons in custom control. – Filip Apr 24 '13 at 06:28

0 Answers0