3

所以我正在运行一个 C# 函数,该函数应该根据值更改文本的颜色。当我从列表视图中删除该函数时,它会输出值但是当我包含它时,它不会输出任何内容我现在终于发现我的函数没有任何问题,但是我如何将我的数据绑定到我的列表视图,所以我想知道我做错了什么。

这是我的代码:

 <asp:SqlDataSource id="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 (2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007 SoundAssist VER 1.0.5  05-12-2011  2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group], [Job Code] AS Job_Code, [TWA], [Job Classification] AS Job_Classification, [Job Function] AS Job_Function, [Job Description] AS Job_Description FROM [Temp Table that contains TWA values] WHERE (([Job Description] = ?) AND ([Group] = ?) AND ([Job Classification] = ?))">
                                                    <SelectParameters>
                                                        <asp:ControlParameter ControlID="DropDownList6" Name="Job_Description" PropertyName="SelectedValue" Type="String" />
                                                        <asp:ControlParameter ControlID="DropDownList4" Name="Group" PropertyName="SelectedValue" Type="String" />
                                                        <asp:ControlParameter ControlID="DropDownList5" Name="Job_Classification" PropertyName="SelectedValue" Type="String" />
                                                    </SelectParameters>

和我的列表视图行:

<asp:ListView id="YourListView"  runat="server" DataSourceID="SqlDataSource3" OnItemDataBound="YourListView_ItemDataBound" >

还有我的颜色功能:

protected void YourListView_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
if (e.Item.ItemType == ListViewItemType.DataItem)
{
    Label theTWALabel = (Label)e.Item.FindControl("TWALabel");
    int theTWAValue = Convert.ToInt32(theTWALabel.Text);
    if (theTWAValue >= 85)
    {
        if (theTWAValue < 90)
        {
            theTWALabel.CssClass = "YellowThis";
        }
        else
        {
            theTWALabel.CssClass = "RedThis";
        }
    }
}
}

这是列表视图:

<ItemTemplate>
    <span style="background-color: white;color: #333333; border: 2em; border-width:1em; border-color:black;"> 
        Plant Name: 
        <asp:Label id="PlantLabel" runat="server" Text='<%# Eval("Plant") %>' />
        <br />
        Department #:
        <asp:Label id="column1Label" runat="server" Text='<%# Eval("column1") %>' />
        <br />
        Department Name:
        <asp:Label id="GroupLabel" runat="server" Text='<%# Eval("Group") %>' />
        <br />
        Job Code:
        <asp:Label id="Job_CodeLabel" runat="server" Text='<%# Eval("Job_Code") %>' />
        <br /> 
        TWA
        <asp:Label id="TWALabel" runat="server" Text='<%# Eval("TWA") %>' />
        <br />
    </span>
</ItemTemplate> 

我也没有输入这些(我的意思是 Sql 语句),我使用内置的 asp.net 连接向导来执行此操作,它为我创建了这一切。

编辑:如果您需要任何其他信息来帮助回答此问题,请发表评论

Edit2:我是否可能需要一个 if 回发功能?

4

3 回答 3

1

您可以尝试遍历控件而不是使用 FindControl。

if (e.Item.ItemType == ListViewItemType.DataItem)
{
    foreach (Control c in e.Item.Controls) 
    {
        if (c is Label && c.ID.Contains("TWALabel"))
        {
            Label theTWALabel = (Label)c
            int theTWAValue = Convert.ToInt32(theTWALabel.Text);
            if (theTWAValue >= 85)
            {
                if (theTWAValue < 90)
                {
                    theTWALabel.CssClass = "YellowThis";
                }
                else
                {
                    theTWALabel.CssClass = "RedThis";
                }
            }            
        }
    }
}

即使 .Net 修改了 ID 名称,您也可以检查 ID 是否具有子字符串 TWALabel 并找到您的控件。

可能有更好的方法来做到这一点,但我想不出另一种我知道会起作用的方法。

于 2013-07-16T13:47:27.470 回答
1

The problem is that you are trying to access the controls in the YourListView_ItemDataBound event handler, and at that point the ListView doesn't have loaded yet.

Try adding to your ListView the event handler onLoad and inside that method then you work with your items like:

protected void YourListView_Load(object sender, EventArgs e)
{
    Label theTWALabel;
    int theTWAValue;
    foreach (ListViewItem item in YourListView.Items)
    {
        theTWALabel = (Label)item.FindControl("TWALabel");
        theTWAValue = Convert.ToInt32(theTWALabel.Text);
        if (theTWAValue >= 85)
        {
            if (theTWAValue < 90)
                theTWALabel.ForeColor = System.Drawing.Color.Yellow;
            else
                theTWALabel.ForeColor = System.Drawing.Color.Red;
        }
    }
}
于 2013-07-16T13:16:10.427 回答
0

YourListView_ItemDataBound 方法在 List 视图被数据绑定时被调用。这是 asp.net 在网格中为数据源中的每一行创建一行的地方,因此您将无法通过像您正在做的那样从控件中拉取回发的数据来访问它。为时已晚,因为旧版本的网格已经被丢弃。

我想你也可以

  • 让 TWALabel 自动回发到服务器并创建一个 OnChange 事件。
  • 解析 Request.Params 以查找每行的回发值
  • 在数据绑定发生之前,尝试在页面生命周期的早期提取您需要的内容。我认为您在 Page_Load 期间仍然可以访问这些数据
于 2013-07-16T14:15:02.857 回答