Hai guys,
I want to find a UL control and then find a LI within that UL and assign a css class to that from a content page....
我想找到一个UL控件,然后在该UL中找到一个LI,并从内容页面中为其分配一个css类....
<ul id="mainMenu" runat="server" style="width:350px;">
<li id="mainHome" runat="server"><a title="Home" href="#" class="home">Home</a></li>
<li id="mainManage" runat="server"><a title="Manage" href="#" class="manage">Manage</a></li>
<li id="mainEnquiry" runat="server"><a title="Enquiry" href="#" class="enquiry">Enquiry</a></li>
<li id="mainReport" runat="server"><a title="Report" href="#" class="report">Reports</a></li>
</ul>
If the user clicks home it is redirected to users.aspx page and i want to highlight Home LI with a color... Plz give me suggestion...
如果用户点击它,它会被重定向到users.aspx页面,我想用一种颜色突出显示Home LI ... Plz给我建议......
2 个解决方案
#1
7
If I have understood this correctly...
如果我理解正确的话......
If your list is on the master page...
如果您的列表在主页面上...
<ul runat="server" id="list">
<li runat="server" id="home">Home</li>
<li runat="server" id="news">News</li>
</ul>
...then you can do this on your content page...
...那么你可以在你的内容页面上做到这一点......
Control list = this.Page.Master.FindControl("list");
Then the li objects will be controls in the list object - e.g. list.Controls
. Or you can do...
那么li对象将是列表对象中的控件 - 例如list.Controls。或者你可以......
Control home = this.Page.Master.FindControl("list").FindControl("home");
...to find specific controls of the list control.
...找到列表控件的特定控件。
When using the runat="server" on the HTML controls the server side equivalent object will be HtmlGenericControl.
在HTML控件上使用runat =“server”时,服务器端的等效对象将是HtmlGenericControl。
If you want to apply a class to the LI tags what you would have to do is cast the LI object to a HtmlGenericControl and then use the Attributes property. For example...
如果要将类应用于LI标记,则必须将LI对象强制转换为HtmlGenericControl,然后使用Attributes属性。例如...
HtmlGenericControl home = (HtmlGenericControl)this.Page.Master.FindControl("list").FindControl("home");
home.Attributes["class"] = "className";
Hope that helps...
希望有帮助......
#2
2
Give this a spin and let me know if it works.
给它一个旋转,让我知道它是否有效。
In CSS, create two classes called something like:
在CSS中,创建两个类,如:
.normalLink
{
background-color:#fff;
}
.selectedLink
{
background-color:#555;
}
In your links:
在您的链接中:
<li id="mainHome" runat="server"><a title="Home" href="users.aspx" class="<%= SetSelectedLink("users.aspx") %>">Home</a>
<li id="mainManage" runat="server"><a title="Manage" href="#" class="<%= SetSelectedLink("manage.aspx") %>">Manage</a></li>
In your code behind page:
在您的代码隐藏页面中:
If you are using a master page, do this next bit in the master code behind, otherwise you can paste it into every regular aspx code behindthat needs it
如果您正在使用母版页,请在后面的主代码中执行此操作,否则您可以将其粘贴到需要它的后面的每个常规aspx代码中
public string SetSelectedLink(string linkURL)
{
if(Request.Url.ToLower().Contains(linkURL.ToLower())))
{
return "selectedLink";
}
else
{
return "normalLink";
}
}
Edit: This only works if you replace your href # with proper urls!
编辑:这只适用于用适当的网址替换你的href#!
#1
7
If I have understood this correctly...
如果我理解正确的话......
If your list is on the master page...
如果您的列表在主页面上...
<ul runat="server" id="list">
<li runat="server" id="home">Home</li>
<li runat="server" id="news">News</li>
</ul>
...then you can do this on your content page...
...那么你可以在你的内容页面上做到这一点......
Control list = this.Page.Master.FindControl("list");
Then the li objects will be controls in the list object - e.g. list.Controls
. Or you can do...
那么li对象将是列表对象中的控件 - 例如list.Controls。或者你可以......
Control home = this.Page.Master.FindControl("list").FindControl("home");
...to find specific controls of the list control.
...找到列表控件的特定控件。
When using the runat="server" on the HTML controls the server side equivalent object will be HtmlGenericControl.
在HTML控件上使用runat =“server”时,服务器端的等效对象将是HtmlGenericControl。
If you want to apply a class to the LI tags what you would have to do is cast the LI object to a HtmlGenericControl and then use the Attributes property. For example...
如果要将类应用于LI标记,则必须将LI对象强制转换为HtmlGenericControl,然后使用Attributes属性。例如...
HtmlGenericControl home = (HtmlGenericControl)this.Page.Master.FindControl("list").FindControl("home");
home.Attributes["class"] = "className";
Hope that helps...
希望有帮助......
#2
2
Give this a spin and let me know if it works.
给它一个旋转,让我知道它是否有效。
In CSS, create two classes called something like:
在CSS中,创建两个类,如:
.normalLink
{
background-color:#fff;
}
.selectedLink
{
background-color:#555;
}
In your links:
在您的链接中:
<li id="mainHome" runat="server"><a title="Home" href="users.aspx" class="<%= SetSelectedLink("users.aspx") %>">Home</a>
<li id="mainManage" runat="server"><a title="Manage" href="#" class="<%= SetSelectedLink("manage.aspx") %>">Manage</a></li>
In your code behind page:
在您的代码隐藏页面中:
If you are using a master page, do this next bit in the master code behind, otherwise you can paste it into every regular aspx code behindthat needs it
如果您正在使用母版页,请在后面的主代码中执行此操作,否则您可以将其粘贴到需要它的后面的每个常规aspx代码中
public string SetSelectedLink(string linkURL)
{
if(Request.Url.ToLower().Contains(linkURL.ToLower())))
{
return "selectedLink";
}
else
{
return "normalLink";
}
}
Edit: This only works if you replace your href # with proper urls!
编辑:这只适用于用适当的网址替换你的href#!