我有一个叫做“ is-active”的类,它有一个彩色的箭头,它会根据用户单击的链接从导航中突出显示到主要内容中。该代码运行aforeach
并从数据库中提取所有类别。如何使“ is-active”类仅针对当前链接显示?我知道它是有效的,因为我将其放入openList
控件中并显示在所有五个类别中,我只是不知道如何使它仅显示在所选类别中。
我尝试附加jQuery来做到这一点,但是linkbutton
在后面的代码中全部添加了,所以我不确定如何附加两者。这是唯一的方法还是另外的方法?
预先感谢您的帮助!
以下是我的类别和链接按钮代码:
protected override void CreateChildControls()
{
LiteralControl openingDiv = new LiteralControl("<div id='MainPanel'>");
LiteralControl closingDiv = new LiteralControl("</div>");
this.Controls.Add(openingDiv);
foreach (DataRow dr in ds.Tables[0].Rows)
{
LiteralControl openList = new LiteralControl("<li class='" + dr["CategoryColor"].ToString() + "'>");
LiteralControl closeList = new LiteralControl("</li>");
Label lblNumber = new Label();
LinkButton myLinkButton = new LinkButton();
myLinkButton.Text = "<span class='number'>" + dr["CategoryNumber"] + "</span>"+ dr["CategoryName"].ToString();
myLinkButton.CommandArgument = dr["Category_ID"].ToString();
myLinkButton.Click += myLinkButton_Click;
this.Controls.Add(openList);
this.Controls.Add(myLinkButton);
this.Controls.Add(closeList);
}
this.Controls.Add(closingDiv);
}
void myLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
Session["CategoryID"] = btn.CommandArgument;
Response.Redirect(Request.RawUrl);
}
这很棘手,因为您在按钮单击处理程序中的response.redirecting会重新创建页面的viewstate。这意味着您的页面将始终显示为新鲜,并且用户单击该链接的事实已经丢失。
作为解决方法,您可以在response.redirect之前将链接ID放在会话变量中,然后在页面重新加载时重新调用它。然后在循环中,如果会话变量与当前按钮instance.id相匹配,则将cssclass设置为is-active。
将cssclass设置为is-active后,请记住也要清除会话变量,以免引起其他页面的混乱。
同样,将按钮添加到控件树后,您将必须进行ID比较,因为这是系统自动为您生成ID的地方。如果您愿意,我可以举一个完整的例子。
请记住,这是一种解决方法,最好使用其他方法。我的意思是,如果要使用ispostback包装而不是重定向,那么在回发时,可以将id设置为isactive更加容易。
如果您希望精通网页,那么页面生命周期是至关重要的,如果您希望精通.net,尤其是提到使用ajax更新面板时。请阅读以下内容:http : //msdn.microsoft.com/zh-cn/library/ms178472(VS.100).aspx,其中包含很多信息,因此请为以后添加书签做准备,因为您将大量使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句