首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改DataList ItemTemplate中的asp:Panels CSS类

更改DataList ItemTemplate中的asp:Panels CSS类
EN

Stack Overflow用户
提问于 2021-10-12 17:42:08
回答 1查看 18关注 0票数 0

我正在尝试创建一个具有多个面板的DataList,并且我想根据DataList的返回结果来更改面板的CSS。我遇到了无法呼叫aspx.cs页面上的ID的问题。

代码语言:javascript
复制
<asp:DataList ID="DataList1" runat="server" DataSourceID="approvalStatus">
            <ItemTemplate>
                    <asp:Panel ID="panel1" runat="server" class="CHANGE_ON_Page_Load>
                        CCB Owners:
                       <asp:Label ID="lbl1" runat="server" Text='<%# Eval("Column1") %>' /><br />
                        Comment:
                       <asp:Label ID="lbl2" runat="server" Text='<%# Eval("Column1") %>' />
                    </asp:Panel>
                    <asp:Panel ID="panel2" runat="server" class="CHANGE_ON_Page_Load">
                       Application Development:
                       <asp:Label ID="lbl3" runat="server" Text='<%# Eval("Column2") %>' /><br />
                       Comment:
                       <asp:Label ID="lbl4" runat="server" Text='<%# Eval("Column2") %>' />
                    </asp:Panel>
            </ItemTemplate>
        </asp:DataList>

我尝试过使用多种方法,但很明显,我不明白我在做什么。以下是我得到的一些尝试和错误。

代码语言:javascript
复制
while (Reader.Read())
                        {
                            string check1 = (Reader["column1"].ToString());
                            string check2= (Reader["column2"].ToString());
                            

                            if (check1 == "Not Checked")
                            {
                                //Show panel with this CSS .approved
                                Panel panel1 = (Panel)this.FindControl("panel1")
                                    panel1.Class = "waitingCell"
                            }
                            else if (check1 == "Approved" || check1  == "Approved With Comments")
                            {
                                //Show panel with this CSS .approved
                                    Panel panel1 = (Panel)this.FindControl("panel1")
                                    panel1.Class = "approvalCell"
                            }
                            else if (check1 == "Rejected" || check1 == "More Information/Meeting Required")
                            {
                                    Panel panel1 = (Panel)this.FindControl("panel1")
                                    panel1.Class = "notapproved"
                                //Show panel with this CSS .notapprovedCell
                            }
                        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 18:05:00

你得到了什么错误?你用什么方法做这件事?您可能希望从ItemDataBound方法中执行此操作,从那里您可以在面板中查找控件并相应地设置css。请参阅下面来自https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.datalist.itemdatabound?view=netframework-4.8的示例

代码语言:javascript
复制
 void Item_Bound(Object sender, DataListItemEventArgs e)
  {

     if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
     {

        // Retrieve the Label control in the current DataListItem.
        Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");

        // Retrieve the text of the CurrencyColumn from the DataListItem
        // and convert the value to a Double.
        Double Price = Convert.ToDouble(
            ((DataRowView)e.Item.DataItem).Row.ItemArray[2].ToString());

        // Format the value as currency and redisplay it in the DataList.
        PriceLabel.Text = Price.ToString("c");

     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69544850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档