目前在维护一个Web应用,用户提出一些对我来说要求比较高的需求,摸索了一下,终于解决了。和大家分享一下:
        问题:一般DataGrid中会有部分选择列,例如:
        <asp:ButtonColumn DataTextField="CaseName" HeaderText="......" CommandName="Select">
                <ItemStyle Height="20px" Width="170px"></ItemStyle>
        </asp:ButtonColumn>
        对于这些列,在ItemDataBound事件中,需要对选择列显示的文本的颜色根据绑定的数据进行处理。使用e.Item.Cells[0].Style.Add("color", "#993300")进行处理。运行时,查看页面代码,发现此属性也加到<TD Style="..."></TD>中了,就是颜色显示不出来。非常郁闷,试了很多方法,都不行。
        仔细看看这个单元格,原来结构如下:<TD Style="..."><A ...>文本</A></TD>。我想应该通过e.Item.Cells[2].Controls[0]将控件取出来,跟了一下代码,.Net将其定义为WebControls.DataGridLinkButton类型。可是此类型在.Net中访问权限受控制。进行e.Item.Cells[2].Controls[0] as WebControls.DataGridLinkButton这样的转型也宣告失败。想想WebControls.DataGridLinkButton肯定是从WebControl继承过来的。于是进行了如下处理:
         WebControl ctr = e.Item.Cells[0].Controls[0] as WebControl;
         ctr.Style.Add("color", "#993300");
        正好可以解决此问题。

 

相关文章:

  • 2022-02-16
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-04-12
  • 2021-12-08
  • 2021-09-29
  • 2021-06-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-10
  • 2022-12-23
  • 2021-07-31
相关资源
相似解决方案