简体   繁体   English

从数据库动态将CSS类绑定到ASP.NET中的div标签C#

[英]bind css class dynamically from database to div tag in asp.net c#

i am creating labels dynamically in list view, the labels are placed inside the div tag and i want the cssclass of the label to be changed for each label. 我在列表视图中动态创建标签,将标签放置在div标签内,并且我希望为每个标签更改标签的cssclass。 i am getting names of the label dynamically from database and that database contains cssclasses for each name. 我正在从数据库中动态获取标签的名称,并且该数据库包含每个名称的cssclasses。 so how can i bind the cssclass dynamically to the div tag? 那么我如何将cssclass动态绑定到div标签呢? following is the code 以下是代码

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
        <div class="sidebar-links">
            <div class="link-blue">
        <a href="#"><i class="fa fa-archive"></i><asp:Label ID="Label1" runat="server" Text='<% #Bind ("mdl_name") %>' ></asp:Label></a>

            </div>
            </div>
    </ItemTemplate>
    </asp:ListView>

This is the codebehind 这是背后的代码

DataTable dt1 = (DataTable)Session["SessionUserInfo"];

String type = dt1.Rows[0]["user_id"].ToString();
SqlConnection conn = new SqlConnection("Data Source=BABA; Database=afaqandco; Integrated Security=true");
SqlCommand cmd = new SqlCommand("SELECT ma.*, m.* FROM tblmdluserassociation AS ma INNER JOIN tbl_module AS m ON ma.mdl_id=m.mdl_id WHERE ma.user_id='"+type+"'", conn);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        DataTable dt = new DataTable();
        da.Fill(dt);

        ListView1.DataSource = dt;
        ListView1.DataBind();

You should add ItemDataBound method to the list view and then create it in code behind as follows: 您应该将ItemDataBound方法添加到列表视图,然后在后面的代码中创建它,如下所示:

        <asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemplaceholder" OnItemDataBound="ListViewMenu_ItemDataBound">
        <LayoutTemplate>
            <asp:PlaceHolder ID="itemplaceholder" runat="server" />
        </LayoutTemplate>
        <ItemTemplate>
            <div class="sidebar-links">
                <div id="MYDIV" runat="server">
                    <a href="#"><i class="fa fa-archive"></i>
                        <asp:Label ID="Label1" runat="server" Text='<% #Bind("mdl_name") %>'></asp:Label></a>

                </div>
            </div>
        </ItemTemplate>
    </asp:ListView>

Code Behind 背后的代码

    protected void ListViewMenu_ItemDataBound(object sender, ListViewItemEventArgs e)
{

    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
        HtmlGenericControl divControl = e.Item.FindControl("MYDIV") as HtmlGenericControl;
        DataRowView dataRow = ((DataRowView)listViewDataItem.DataItem);
        divControl.Attributes.Add("class", dataRow["CLASS"].ToString());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从动态添加的标签中检索数据,asp.net c#中动态添加的div标签中的下拉列表 - how to retrieve data from dynamically added label,drop down list in dynamically added div tag in asp.net c# 在foreach中从数据库中调用Div-ASP.NET C# - Summoning Div from database in foreach - asp.net c# Html,C#,ASP.NET如何重复包含标签的div标签中的内容来显示数据库中的所有记录? - Html, C#, ASP.NET How can i repeat the contents in div tag containing labels to display all records from database? 如何在ASP.NET控件中的div标签中使用类控制InnerHtml C# - How to use class in div tag inside asp.net controls InnerHtml c# 在ASP.NET C#中动态创建/复制的Div - Dynamically Created/Copied Div in asp.net C# 使用C#,MSSQL将值从Asp.net中的数据库绑定到复选框 - Bind value to checkbox from database in Asp.net using C#, MSSQL 如何使用c#从asp.net中的数据库绑定图像? - how to bind image from database in asp.net using c#? 从字典 asp.net C# 中绑定 GridView - Bind GridView from dictionary asp.net C# 在ASP.NET C#中动态填充数据库中的下拉列表 - fill drop down list from database dynamically in asp.net c# ASP.NET C#如何通过从数据库检索的字符串动态选择按文本列出项目 - ASP.NET C# How to Select List Item by Text dynamically through string retrieved from database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM