使用;用于导航(无法记住状态)
我有一个使用 ul 和 li 的导航菜单。我可以填充它,但一旦发生回发或移动到新页面,它就无法记住其状态。
这是代码:
Masterpage.Master
<ul id="main-nav">
<li>
<a href="MPTest2.aspx" class="nav-top-item" runat="server">
Employees
</a>
<ul id="mgList" runat="server" >
</ul>
</li>
</ul>
MasterPage.Master.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(@"Data Source= localhost;Initial Catalog=ServerDb;Integrated Security=True");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("select EmpName, EmpID from Employee order by EmpName", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
connection.Close();
if (dt.Rows.Count > 0)
{
HtmlGenericControl li;
for (int j = 0; j < dt.Rows.Count; j++)
{
li = new HtmlGenericControl("li");
HtmlAnchor a = new HtmlAnchor();
a.HRef = "~/MPTest1.aspx?MGID=" + Convert.ToInt32(dt.Rows[j]["EmpID"]);
a.InnerText = dt.Rows[j]["EmpName"].ToString();
li.Controls.Add(a);
mgList.Controls.Add(li);
}
}
}
JQuery:
$(document).ready(function(){
//Sidebar Accordion Menu:
$("#main-nav li ul").hide(); // Hide all sub menus
$("#main-nav li a.nav-top-item").click( // When a top menu item is clicked...
function() {
$(this).parent().siblings().find("ul").slideUp("normal"); // Slide up all sub menus except the one clicked
$(this).next().slideToggle("normal"); // Slide down the clicked sub menu
return false;
}
); }
现在我知道 hide() 函数隐藏了所有菜单需要第一次调用。但之后我需要一个类或函数来记住菜单的状态(如果它是打开的)而不是折叠所有内容。
如果我能得到一些建议或一些例子,那就太好了。
谢谢
I have a Navigation menu using ul and li.. i am able to populate it but it cannot remember its state once postback happens or if i move to a new page.
Here is the code:
Masterpage.Master
<ul id="main-nav">
<li>
<a href="MPTest2.aspx" class="nav-top-item" runat="server">
Employees
</a>
<ul id="mgList" runat="server" >
</ul>
</li>
</ul>
MasterPage.Master.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(@"Data Source= localhost;Initial Catalog=ServerDb;Integrated Security=True");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("select EmpName, EmpID from Employee order by EmpName", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
connection.Close();
if (dt.Rows.Count > 0)
{
HtmlGenericControl li;
for (int j = 0; j < dt.Rows.Count; j++)
{
li = new HtmlGenericControl("li");
HtmlAnchor a = new HtmlAnchor();
a.HRef = "~/MPTest1.aspx?MGID=" + Convert.ToInt32(dt.Rows[j]["EmpID"]);
a.InnerText = dt.Rows[j]["EmpName"].ToString();
li.Controls.Add(a);
mgList.Controls.Add(li);
}
}
}
JQuery:
$(document).ready(function(){
//Sidebar Accordion Menu:
$("#main-nav li ul").hide(); // Hide all sub menus
$("#main-nav li a.nav-top-item").click( // When a top menu item is clicked...
function() {
$(this).parent().siblings().find("ul").slideUp("normal"); // Slide up all sub menus except the one clicked
$(this).next().slideToggle("normal"); // Slide down the clicked sub menu
return false;
}
); }
Now i know the hide() function hides all the menus which is needed to be called the first time. But after that i need a class or function which remembers the state of the menu if it was open rather than collapsing everything.
It would be great if i could get some suggestions or some examples.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这并不完全是您正在寻找的东西,但它是我用来跟踪我的菜单/子菜单活动的东西。我相信您应该能够使用我在下面提供的前提来显示正确的菜单项。我也可能不理解这个问题,如果是这样,请告诉我,我会尽力提供进一步的帮助。下面的示例与 Razor MVC3 一起使用。
我还想指出,我将自己限制为这段代码的两级菜单,如果您使用的菜单超出了两级,则应该修改代码以使其递归更加稳健。
在您下面评论的主题中,我相信您可以做类似以下的事情,这并不完美,但可能会给您一个正确方向的开始。
I know this isn't exactly what you are looking for but it is what I use to track my menu/submenu activeness. I believe you should be able to use the premise I provide below to show the correct menu items. I might also not be understanding the question, if that is the case please let me know and I'll try to help further. The example below is being used with Razor MVC3.
I'd also like to point out that I have limited myself to two levels of menus for this code and if you use more than that you should modify the code to be more recursively robust.
In the theme of your below comment I believe you could do something like the following which is not perfect but may give you a start in the right direction.
您可以使用 cookie 来存储状态更改,并在页面加载时重新加载它。如果您不想诉诸默认的 JavaScript 内容,可以使用一个用于 cookie 管理的 jQuery 插件。
You could use a cookie to store the state as it changes, and re-load it on page load. There's a jQuery plugin for cookie management if you don't want to resort to the default JavaScript stuff.