gpt4 book ai didi

ASP.Net - 命名容器和 ClientIDMode ="Static"

转载 作者:行者123 更新时间:2023-12-02 15:45:42 24 4
gpt4 key购买 nike

MSDN 文档中有关 ClientIdMode 的说明:

Static The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

来源:http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx

但是,我并不认为“顶层”业务是这样的。例如,我有一个用户控件:

<uc1:WidgetsListControl runat="server" id="WidgetsListControl" ClientIDMode="Static"  />

为了更好地衡量,我也在控制源中设置了 clientidmode,尽管我不确定需要哪一个:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WidgetsListControl.ascx.cs" Inherits="WebApplication1.WidgetsListControl" EnableViewState="false" ClientIDMode="Static" %>

在用户控件中,我有一个文本框:

<asp:TextBox ID="testTextBox" runat="server" />

我的期望是文本框的名称类似于 WidgetsListControl$testTextBox

但是我在查看源代码时发现的是:

<input name="ctl00$MainContent$WidgetsListControl$testTextBox" id="testTextBox" type="text"/>

我错过了什么?有没有一种方法可以实现我正在寻找的内容(较短的 ID),而无需在用户控件中的每个控件上设置“静态”?

编辑:

实际上,仔细观察后,我发现 ID 属性按照 MSDN 中的描述工作 - 但名称属性仍然是命名容器层次结构的完整串联。

鉴于站点高度复杂,这些控件的名称和 ID 开始占用大部分带宽(标记大小)。我似乎找不到任何好的解决方法来减少这方面的标记。

最佳答案

您必须在面板或带有runat="server"的东西中拥有此控件自定义控件本身是其子控件的命名容器。如果您希望标识符保持不变的命名方式,则需要使用 Predictable (IIRC) 客户端 ID 模式。

注意:尽管查看 Predictable 的文档情况似乎并非如此,我确信这就是我一直用来在 ASP.NET 中获取“干净”.NET 标识符的方法。

此外,如果要全局应用 ClientIDMode,请在 web.config 文件中 <pages> 的属性中指定它。元素。

关于ASP.Net - 命名容器和 ClientIDMode ="Static",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com