Dynamically change the class of a div nested in a gridview item template from code behind c# [closed]

只愿长相守 提交于 2019-12-31 07:43:07

问题


I need to change the class of a div nested inside a gridview item template, i have given the runat="server" tag and an id for the div. How can we change the class of that particular div upon gridview databind based on each row conditions.


回答1:


ASPX

<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <div id="yourDiv" runat="server"></div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        HtmlGenericControl div = (HtmlGenericControl)e.Row.FindControl("yourDiv");
        div.Attributes.Add("class", "ClassYouWantToAdd");
    }
}


来源:https://stackoverflow.com/questions/31719965/dynamically-change-the-class-of-a-div-nested-in-a-gridview-item-template-from-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!