【问题标题】:Changing user control css class in code behind在后面的代码中更改用户控件 css 类
【发布时间】:2012-06-28 16:58:32
【问题描述】:

所以我有一个显示链接的用户控件,我想更改当前活动链接的颜色。 这是我的用户控件 ascx 文件的 ListView 中的代码。

<asp:ListView ID="sidebarListView" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSourceSidebar" OnItemCommand="sidebarListView_ItemCommand">
<ItemTemplate>
  <div id="sidebarItemID" class="sidebarItem" runat="server">
     <div>
        <asp:LinkButton ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' CommandName="Select" CommandArgument='<%# Eval("Id") %>' />
     </div>
  </div>
</ItemTemplate>
....

单击链接按钮时,我需要更改 sidebarItemID 类。 我后面的 default.aspx 代码如下所示:

private void SideBar1_ItemCommand ( object sender , EventArgs e ) {
  Int32 facId = Sidebar1.FacultyId;
  SqlDataSource1.SelectCommand = "SELECT [Id], [Name], [Faculty_Id], [User_Id], [Author], [Picture], [Location] FROM [Books] WHERE [Faculty_Id]=" + facId + " ORDER BY [DateAdded] DESC";
  HtmlGenericControl htmlDivControl = (HtmlGenericControl) FindControlRecursive( Sidebar1 , "sidebarItemID" );
  htmlDivControl.Attributes.Add("class", "sidebarItemActive");
  string newClass = htmlDivControl.Attributes["class"];
  //Response.Write( String.Format( "<script>alert('{0}');</script>" , newClass ) );
}

这会根据在用户控件中单击的链接的 ID 正确更改 sqlDataSource。但是类没有改变。

如果我取消注释 Response.Write(...) 部分,它会正确显示“sidebarItemActive”的警报,因此它会正确地从我的页面中找到控件并将其类属性分配为“sidebarItemActive”,但页面上没有任何变化如果我在浏览器中查看页面源代码,它会说该类仍然是“sidebarItem”。 更改没有生效是怎么回事?

【问题讨论】:

  • 也许属性已经存在,添加它并没有如你所愿生效?试试htmlDivControl.Attributes["class"] = "sidebarItemActive"; 编辑:这实际上可能行不通。检查页面上 div 的 ID 是否真的是 sidebarItemID,因为控件可以将其控件名称附加到 id。
  • 我试过了,同样的。此外,据我所知,Add 的行为与 Put 相同,因此如果它已设置,它将替换该值。
  • 嗯。在页面上,ID确实不是“SidebarItemID”而是“sidebar_Sidebar1_sidebarListView_sidebarItemID_0”等。但是findcontrol方法实际上是如何找到元素的呢?另外,如果我将用户控件 ClientIDMode 设置为静态,它仍然不起作用,但 ListView 中的所有项目都具有相同的 ID,但类仍然没有改变
  • 我认为这与页面生命周期有关。回发是在渲染之前处理的,并且由于您为 div 使用模板,因此在处理代码之后会创建实际的侧边栏 div 集。尝试在控件的 Render 上执行此操作。如果这如我所愿,我会将其作为答案提交
  • 嗯,我不确定我是否理解。基本上,我不能在控件的渲染上这样做,因为当控件被渲染时,我不需要更改类。当用户单击控件中的链接时,我需要更改它。我理解正确吗?感谢您的帮助

标签: c# asp.net user-controls


【解决方案1】:

您页面上的实际&lt;div&gt;s 是在ASP.NET Page Life Cycle 的渲染状态期间生成的。这发生在 Control 事件处理阶段之后,因此您在处理程序函数期间对 HTML 所做的任何更改都在 Render 期间有效地撤消,因为控件的 HTML 是从模板重新生成的。在这种情况下,要让控件更改其内部 HTML,您可以将更新代码放在控件的重写 Render 函数中。 MSDN 上有一个example 说明如何编写渲染函数。

【讨论】:

    【解决方案2】:

    所以我通过定义 ListViews ItemDataBound 事件处理程序并在其中分配 css 类来解决问题。以前的问题是,由于我的控件在 updatePanels 内,所以 ItemDataBound 事件没有触发。我不得不在 click 方法中手动调用“DataBind()”。

    在我的 default.aspx 中

    // Exposed click event, updates RecentBooks control sqlDataSource.
    private void SidebarItems1_Clicked ( object sender , EventArgs e ) {
      RecentBooks1.updateDataSource( SidebarItems1.FacultyId.ToString() );
      SidebarItems1.DataBind();
    

    }

    在我的用户 control.ascx 中

    protected void sidebarListView_ItemDataBound ( object sender , ListViewItemEventArgs e ) {
      LinkButton button = e.Item.FindControl( "NameLabel" ) as LinkButton;
      if (FacultyName != null && button != null) {
         if ( button.Text == FacultyName ) {
            button.CssClass = "sidebarItemActive";
         }
      }
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多