Search code examples
.netc#-4.0.net-4.0clientidasprepeater

Can repeaters automatically use a different clientIDMode then what's set in web.config? (clientIDMode="Static")


I want to use clientIDMode="Static" in the web.config as it's awesome for front end development, obivously.

But, I'd like my repeaters, datagrids, and datalists to default to clientIDMode="Predictable" so there's not duplicate ID's on the page. I'd prefer not to have to set this every time I create a repeater because it's extra code and if I forget I won't see any problems immediately so I'll probably continue on without realizing I made a mistake.

Any help on this issue would be greatly appreciated.


Solution

  • No, i'm afraid such a thing doesn't exist.

    If it's really important for you, you could implement following in your Master's or Page's Page_Init:

    protected void Page_Init(object sender, EventArgs e)
    {
        var types = new Type[] { 
            typeof(Repeater), typeof(DataGrid), typeof(GridView), 
            typeof(DataList), typeof(ListView), typeof(FormView) 
        };
        var allControls = new List<Control>();
        FindChildControlsRecursive(Page, types, allControls);
        foreach (var ctrl in allControls)
        {
            ctrl.ClientIDMode = ClientIDMode.Predictable;
        }
    }
    
    public void FindChildControlsRecursive(Control control, IList<Type> types, IList<Control> result)
    {
        foreach (Control childControl in control.Controls)
        {
            var controlType = childControl.GetType();
            if (typeof(Control).IsAssignableFrom(controlType) && types.Contains(controlType))
            {
                result.Add((Control)childControl);
            }
            else
            {
                FindChildControlsRecursive(childControl, types, result);
            }
        }
    }
    

    But in my opinion this is too much for a reminder.