簡體   English   中英

我的列表視圖不會輸出,是否需要回帖?

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

所以我正在運行一個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 post back功能嗎?

問題是您正在嘗試訪問YourListView_ItemDataBound事件處理程序中的控件,此時ListView尚未加載。

嘗試向ListView添加事件處理程序onLoad並在該方法中添加您的項目,如:

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

您可以嘗試循環控件而不是使用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並找到您的控件。

可能有更好的方法,但我想不出我知道的另一種方式。

調用YourListView_ItemDataBound方法,因為List視圖是數據綁定的。 這就是asp.net在您的網格中為數據源中的每一行創建一行的地方,因此您將無法像控制中那樣從控件中提取數據來訪問這些數據。 現在為時已晚,因為網格的舊版本已被丟棄。

我想你也可以

  • 讓TWALabel自動回復到服務器並創建一個OnChange事件。
  • 解析Request.Params以查找每行的回發值
  • 在數據綁定發生之前,嘗試並在頁面生命周期中提取您需要的內容。 我認為你在Page_Load期間仍然可以訪問這些數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM