I am trying to create a DataList with multiple panels, and I want to change the Panel's CSS based on the returning results of the DataList. I am having a problem with not being able to call the ID on the aspx.cs page.
<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>
I have tried using multiple methods, and clearly, I do not understand what I am doing. Here are some of the tries and errors that I am getting.
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
}
}
What errors are you getting? What method are you doing this in? You will probably want to do it form the ItemDataBound method, from there you can look up your controls in the panels and set css accordingly. See example below from https://learn.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");
}