我的标题控件中有一个用于导航的无序列表。在我的每个其他页面上调用标题控件。
<ul>
<li class="active">Home</li>
<li>About</li>
<li>Store</li>
</ul>
加载时以编程方式将活动类更改为“关于”页面的最佳方法是什么?
我在这个项目中使用 C#。
Just add a public property to the user control.
I assume this data is in a data structure when your page is initialized. The property type should be of the appropriate type to identify that data. Then when the page is rendered, the control should take the value of the property into consideration for how the data is rendered.
This might involved setting runat=server
for each list item and then setting the CssClass
property for the appropriate one. I personally took a different approach, writing the raw code to generate the HTML. I can think of other options as well.
An example of the first approach might look something like this (untested):
ascx
<ul>
<li id="HomeList" runat="server">Home</li>
<li id="AboutList" runat="server">About</li>
<li id="StoreList" runat="server">Store</li>
</ul>
User Control cs
public string ActiveItem { get; set; }
protected void Page_PreRender(object sender, EventArgs e)
{
if (ActiveItem != null)
{
if (ActiveItem == "Home")
HomeList.CssClass = "active";
if (ActiveItem == "About")
HomeAbout.CssClass = "active";
if (ActiveItem == "Store")
HomeStore.CssClass = "active";
}
}
Page cs
UserControl1.ActiveItem = "Home";
Getting much more specific would probably require more information about those data structures and the inner working of your user control.