根据另一列中的值在ListView上有条件地更改样式

时间:2015-09-28 22:00:01

标签: asp.net listview formatting conditional datarow

如果表格中添加的列的绝对值小于8,我试图更改行上的背景颜色。我尝试了几种不同的方法,但最近的尝试虽然看似很接近但不会像我期待。



    <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>
&#13;
&#13;
&#13;

和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();
            }

        }

    }

}

我在哪里误解了如何更新最后一栏?

1 个答案:

答案 0 :(得分:0)

我发现我必须使用有效的JavaScript解决方案。

@Class = ClassSchedDropDown.SelectedValue;
            if (RegionSchedDropDown.SelectedValue == "")
            {
                for (int i = 1; i <= 8; i++) 
                {
                    SchedScoreDirect = "CurrentSchedScoreClassReg";
                    @Reg = i;
                }
            }

SchedScoreDirect =“CurrentSchedScoreClassReg代码将指示处理将红色背景发送到适当的行。