0
如果表中添加的列的絕對值小於8,我試圖更改一行上的背景色。我嘗試了幾種不同的方法,但儘管看起來很接近,但最新的嘗試沒有像我預期的那樣表現出色。根據另一列中的值在ListView上有條件地更改樣式
<asp:ListView ID="ListView2" runat="server" >
<LayoutTemplate>
<table id="Table2" style="border:solid" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table runat="server" id="itemPlaceholderContainer" border="0" >
<tr id="Tr2" runat="server" style='<%# Eval("Alert") %>'>
<th id="Th1" runat="server">Date</th>
<th id="Th2" runat="server">Home</th>
<th id="Th3" runat="server">Score</th>
<th id="Th4" runat="server">Away</th>
<th id="Th5" runat="server">Score</th>
<th id="Th6" runat="server">Quarter</th>
<th id="Th7" runat="server">Link To Game</th>
<th id="Th8" runat="server"></th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<AlternatingItemTemplate>
<tr style="">
<td style="border:solid">
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HomeTeamScore") %>' runat="server" ID="HomeTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("AwayTeamScore") %>' runat="server" ID="AwayTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("Quarter") %>' runat="server" ID="LabelQuarter" /></td>
<td style="border:solid" id="HomeAudioLink">
<a href="<%# Eval("AudioInternetLink") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink") %>' runat="server" ID="LabelHomeAudioLink" />
</a>
</td>
<td style="border:solid" id="AwayAudioLink">
<a href="<%# Eval("AudioInternetLink1") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink1") %>' runat="server" ID="LabelAwayAudioLink" />
</a>
</td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr id="Game" style="" runat="server" class="" >
<td style="border:solid">
<asp:Label Text='<%# Eval("Date") %>' runat="server" ID="DateLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName") %>' runat="server" ID="HighSchoolNameLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HomeTeamScore") %>' runat="server" ID="HomeTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("HighSchoolName1") %>' runat="server" ID="HighSchoolName1Label2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("AwayTeamScore") %>' runat="server" ID="AwayTeamScoreLabel2" /></td>
<td style="border:solid">
<asp:Label Text='<%# Eval("Quarter") %>' runat="server" ID="LabelQuarter" /></td>
<td style="border:solid" id="HomeAudioLink">
<a href="<%# Eval("AudioInternetLink") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink") %>' runat="server" ID="LabelHomeAudioLink" />
</a>
</td>
<td style="border:solid" id="AwayAudioLink">
<a href="<%# Eval("AudioInternetLink1") %>" target="_blank" >
<asp:Label Text='<%# Eval("AudioInternetLink1") %>' runat="server" ID="LabelAwayAudioLink" />
</a>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
和C#代碼:
protected void ScheduleButton_Click(object sender, EventArgs e)
{
/**********************************************************************/
/* The code below will initialize the connection to the database. */
/* As the connection string to the SQL database is defined as conn, */
/* the open method from conn will connect to the database, and the */
/* cmd variable will call on the stored procedure GetSchedule. */
/**********************************************************************/
string strcon = WebConfigurationManager.ConnectionStrings["FollowingHSFootballConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
SqlCommand cmd = new SqlCommand("CurrentSchedScore", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
DataTable ScheduleTbl = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// Fill the partial DataSet into a partial DataTable.
da.Fill(ScheduleTbl);
ListView2.DataSource = ScheduleTbl;
ListView2.DataBind();
// Close Game Logic
ScheduleTbl.Columns.Add("Diff", typeof(int), "HomeTeamScore - AwayTeamScore");
ScheduleTbl.Columns.Add("Alert", typeof(string), "");
for (int i = 0; i < ScheduleTbl.Rows.Count; i++)
{
DataRow drST = ScheduleTbl.Rows[i];
if (Math.Abs((int)drST.ItemArray.GetValue(8)) <= 8)
{
drST.BeginEdit();
drST.ItemArray.SetValue("background-color:red", 9);
drST.AcceptChanges();
drST.EndEdit();
}
}
}
}
如果我誤解了如何更新的最後一列?