简体   繁体   English

将用户控件添加到用户控件时丢失样式

[英]Losing styles when adding a User Control to a User Control

In my project, I am trying to add a button collection user control to an input based user control. 在我的项目中,我试图将按钮集合用户控件添加到基于输入的用户控件中。 The button uc is going to be added to numerous other controls. 按钮uc将被添加到许多其他控件中。 The problem that I've encountered is that the button uc is losing whatever css styles that I've attach to it, once it is added to the parent uc. 我遇到的问题是,一旦按钮uc被添加到父uc中,它就会丢失我附加到其上的所有css样式。 Initially, I had a panel on the parent that would add the button uc when the page was loaded, then I would add the button uc to the panel: 最初,我在父面板上有一个面板,该面板将在页面加载时添加按钮uc,然后将按钮uc添加到面板中:

(panel.controls.add(uc)

This did not work, the styling of the buttons were lost. 这不起作用,按钮的样式丢失了。 Next, I tried to set the positioning to the button uc and add it directly to the form: 接下来,我尝试将定位设置为按钮uc并将其直接添加到表单中:

static void SetLocation(Usercontrol uc) {uc.Attributes.add("style","left:425px;top:420px;"); }   
public void SetPage() { button = (uc)LoadControl(button.ascx); SetLocation(buttonuc); this.Controls.Add(buttonuc); }

But, this did not work either, it did not even locate the control properly. 但是,这也不起作用,甚至无法正确定位控件。 My style sheet is already added to the parent uc, and the button uc to be added has its style on that sheet. 我的样式表已经添加到父uc中,要添加的按钮uc在该表上具有其样式。 I don't want to have to manually add the buttons to each uc parent. 我不想手动将按钮添加到每个uc父级。 Thank you for any help. 感谢您的任何帮助。 I sure I'm just missing something obvious, but I've yet to come upon a similar issue in a search. 我确定我只是缺少明显的东西,但是在搜索中还没有遇到类似的问题。 Thank you. 谢谢。

In general it is good practice to keep your styles separate from the markup. 通常,最好将样式与标记分开。 This makes maintenance easier and it also saves yourself from searching for the reason one of your elements appears a pixel away from where you are expecting (for example... Similar issues can be exasperating). 这使维护变得更容易,并且还使您避免寻找原因,即其中一个元素的像素距离预期位置较远(例如,类似的问题可能会令人生厌)。

Do you have the same issue if you set the CssClass attribute for your button(s) and set the styles in a css file for the class? 如果您为按钮设置C​​ssClass属性并为该类的css文件设置样式,是否会遇到相同的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM