0

我想在 web.config 中使用 clientIDMode="Static",因为它对于前端开发来说非常棒,很明显。

但是,我希望我的中继器、数据网格和数据列表默认为 clientIDMode="Predictable",这样页面上就没有重复的 ID。我不希望每次创建转发器时都必须设置它,因为它是额外的代码,如果我忘记了,我不会立即看到任何问题,所以我可能会继续,而不会意识到我犯了错误。

对此问题的任何帮助将不胜感激。

4

1 回答 1

1

不,恐怕这种事情不存在。

如果这对您来说真的很重要,您可以在您的硕士或页面中实现以下内容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);
        }
    }
}

但在我看来,这个提醒太多了。

于 2012-04-18T20:30:29.723 回答