简体   繁体   中英

My listview won't output, is a if post back necessary?

so I am running a C# function in that is supposed to change the colors of the text based on the value. When I removed the function from the list view it would output the values but when I included it, it would output nothing I have now finally figured out that there is nothing wrong with my function but with how i am binding my data to my list view, so I am wondering what is it I am doing wrong.

Here is my code:

 <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>

and my list view line:

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

And my color function:

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";
        }
    }
}
}

And here is the List View:

<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> 

Also I didn't type these in (The Sql statement I mean), I used the built in asp.net connection wizard to do this, and it created it all for me.

Edit: If there is any other info you need to help answer this question, please comment

Edit2: Is it possible that I need a if post back function?

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;
        }
    }
}

You could try to loop through the controls rather than using 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";
                }
            }            
        }
    }
}

Even if .Net modifies the ID name, you can check to see if the ID has the substring TWALabel and find your control that.

There is probably a better way to do it, but I can't think of another way that I know will work.

The YourListView_ItemDataBound method is called as the List view is being data bound. This is at the point where asp.net is creating a row in your grid for each row in your data source, so you will not be able to access the posted back data by pulling it from the control like you are doing. It is too late, as that old version of the grid has already been discarded.

I think you can either

  • Have the TWALabel autopostback to the server and create an OnChange event.
  • Parse the Request.Params to find the posted back value for each row
  • Try and pull what you need earlier in the page lifecycle before the databinding occurs. I think you would still have access to this data during Page_Load

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM