开发者

How to set Datagrid CSS classes from Codebehind in ASP.NET?

I have a datagrid where I am setting CSS for ItemStyle, Header style and Alternating item style in .aspx page markup as follows:

 <asp:DataGrid runat="server" ID="dgScannedEsn" AutoGenerateColumns="True"  CssClass="gridCls" HeaderStyle-Css开发者_运维知识库Class ="clsItemHeader" AlternatingItemStyle-CssClass ="clsAlternateItemRow"" ItemStyle-CssClass ="clsItemRow"  ></asp:DataGrid>

How can I set these CSS classes (HeaderStyle-CssClass, AlternatingItemStyle-CssClass, etc) from codebehind ?


Use the Attributes property:

void Page_Load(Object sender, EventArgs e) {
           datagrid1.Attributes["HeaderStyle-CssClass"]="clsItemHeader";

        }


Yes. Depending on how granular you want to get, you can set most of them in the Page_Init event. Each one of those properties will be accessible directly in the Intellisense.They won't have the same exact names because the code behind won't accept the "-" in the name, but they are all there.

If you want to change them during your databinding (like a different style for each row for each column etc), you can do that during the RowCreated event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜