我正在尝试创建一个具有多个面板的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
}
}发布于 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的示例
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");
}https://stackoverflow.com/questions/69544850
复制相似问题