更改 DataList ItemTemplate 中的 asp:Panels CSS Class
Changing an asp:Panels CSS Class that is within a DataList ItemTemplate
我正在尝试创建一个包含多个面板的 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
}
}
你遇到了什么错误?你用什么方法做这个?您可能希望通过 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");
}
我正在尝试创建一个包含多个面板的 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
}
}
你遇到了什么错误?你用什么方法做这个?您可能希望通过 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");
}