asp.net:防止生成服务器控制 ID

发布于 2024-07-14 01:30:49 字数 531 浏览 5 评论 0原文

问题

当使用 asp.net 服务器控件时,会自动生成如下所示的 id 属性。

<img id="ctl00_body_ULRepeater_ctl01_LIRepeater_ctl00_PartImg" src="img.png" />

虽然我一般并不反对 id 属性,但我尝试避免使用这些不必要的冗长类型的名称,并使用简洁的描述性名称。

问题

我可以阻止 asp.net 生成这些 id 属性吗? 它们看起来很糟糕,如果我用中继器或其他东西生成很多项目,它们实际上会增加很多页面重量。 我该如何摆脱它们?

注释

我在 Visual Studio 2008 中使用 asp.net 3.0。

[更新]

好的,所以我可以子类化(ClientID 被声明为可重写),但这确实没什么乐趣。 我可以在任何地方使用文字控件。 或者我可以咬紧牙关,忍受几乎没有任何内容的页面的缓慢渲染。

The Problem

When using asp.net server controls id attributes such as the following are automatically generated.

<img id="ctl00_body_ULRepeater_ctl01_LIRepeater_ctl00_PartImg" src="img.png" />

While I'm not averse to id attributes in general, I try to stay away from using these unnecessarily verbose types of names and use concise, descriptive names.

The Question

Can I stop asp.net from generating these id attributes? They look terrible, and if I generate a lot of items with a repeater or something they actually add a good bit of page weight. How do I get rid of them?

Notes

I am using asp.net 3.0 in Visual Studio 2008.

[update]

Ok, so I can subclass (ClientID is declared overridable), but this is no fun really. I can use Literal Controls everywhere. Or I can grit my teeth and bear the painfully slow rendering of my pages with nearly nothing on them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明明#如月 2024-07-21 01:30:49

我相信 ASP.NET 4.0 中的功能之一是能够更好地控制生成的 ID。 现在,您将获得任何服务器生成的控件的名称修改。 这使得 ASP.NET 能够保证控件 ID 的唯一性。

您始终可以使用直接 HTML 标记(不要 runat=server)来避免此问题。 不过,您会为了轻量级页面而牺牲易用性。

I believe that one of the features coming in asp.net 4.0 will be the ability to better control the IDs that are generated. For now, you are going to get the name mangling for any server generated control. This is what allows asp.net to guarantee the uniqueness of your control's ID.

You can always use straight HTML markup (do not runat=server) to avoid this issue. You would be sacrificing the ease of use for a lighter weight page though.

通知家属抬走 2024-07-21 01:30:49

您可以通过在运行时将每个控件的 Id 属性设置为 null 来完全隐藏这些 id 属性,例如

Private Sub repeNewsletters_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repeNewsletters.ItemDataBound

    If e.Item.DataItem Is Nothing Then
        Return
    End If

    Dim hlDetails = DirectCast(e.Item.FindControl("hlDetails"), System.Web.UI.WebControls.HyperLink)

    hlDetails.ID = Nothing

end sub

You can hide these id attributes completely by setting each control's Id property to null at runtime, e.g.

Private Sub repeNewsletters_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repeNewsletters.ItemDataBound

    If e.Item.DataItem Is Nothing Then
        Return
    End If

    Dim hlDetails = DirectCast(e.Item.FindControl("hlDetails"), System.Web.UI.WebControls.HyperLink)

    hlDetails.ID = Nothing

end sub

做个ˇ局外人 2024-07-21 01:30:49

截至目前,ControlID 是只读属性。 在即将发布的 ASP.NET Web 窗体(带有 .NET 4.0)中,这将是使用许多不同方法(例如静态、继承等)的可设置属性。

As of now ControlID's are ReadOnly properties. In the upcoming release of ASP.NET Web Forms (with .NET 4.0) this will be a settable property using a number of different methods (such as static, inherit, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文