开发者

is there a simple way to remove the "ct100" prefix from a .NET user control?

开发者 https://www.devze.com 2023-03-29 06:37 出处:网络
Long story short, dozens of pages use no master page.For a new module I created a master page with a menu control (menu control exists already) so I can get the same look across the six or so pages I\

Long story short, dozens of pages use no master page. For a new module I created a master page with a menu control (menu control exists already) so I can get the same look across the six or so pages I'm creating now. Since the content page uses the master page, the Menu control has its name changed to ct100_Menu1 instead of just Menu1. Wouldn't be an issue except someone decided to use the exact name of the control for the CSS that styles the menu, by its exact ID (e.g. CSS is Menu1 a { /* stuff */ }). So the menu won't render properly because I'm using a Master Page and not just copying the code.

I cannot change the CSS code in the menu file as it could break something, so is there any way that I can change the control to not display that pesky ct100 without having to add any tools or mess with creating my own custom control (as I can't replace the Menu.ascx control, although I might be able to modify it to add CSS classes) or is my only choice to either not use a master page or copy the menu CSS into another file and 开发者_如何学Pythonset it properly?

Feel kind of stuck between a rock and a hard place because the code was deliberately written so you cannot use Master Pages and nobody ever went back to change it.


You should set the ClientIdMode to Static. Here's more information from MSDN. Note: This is .NET 4.0 only.

In earlier versions, I would recommend styling off of classes as you can't really control what the name will be everywhere that you use it (as you found out).


If you are on ASP.net 4.0, you can set the ClientID property of the controls.

Otherwise, you're in for a world of hurt as in: Custom Control, ASP.net Literals or JavaScript to change the IDs.


Are you using .NET 4? If so, you can set this on your control:

 <asp:SomeControl ClientIDMode="Static" />


Just add the new name to the CSS - without removing the old (since you said that was an issue):

 ctl100_Menu1 a, 
 Menu1 a { /* stuff */ }


If you are using ASP.NET 4.0, you can override the ClientId rendering mode, either per control, or for all controls. For instance:

<my:Menu runat="server" Id="Menu1" ClientIDMode="Static" />

This will enforce that the value "Menu1" is preserved as the client side Id for the element that is rendered. (see here).

What I would recommend though, is apply a CSS class to the menu element, and then adjust the CSS rules around a class. E.g.,:

#Menu1 a {

... to:

#Menu1 a,
div.menu a {

... etc


To "correct" this behavior for your entire web application, look in your web.config for the following tag:

<system.web>
    ...
    <pages ... clientIDMode="*something*">
    </pages>
    ...
</system.web>

Remove the clientIDMode="*something*" property specification. Just take it out.

Yay.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号