【问题标题】:Changing an asp:Panels CSS Class that is within a DataList ItemTemplate更改 DataList ItemTemplate 中的 asp:Panels CSS 类
【发布时间】:2021-12-01 07:19:03
【问题描述】:

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

<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>

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

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
                            }
                        }

【问题讨论】:

    标签: c# asp.net panel datalist


    【解决方案1】:

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

     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");
    
         }
    

    【讨论】:

      猜你喜欢
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多