Listview正在丢失有关回发ASP.Net的数据

时间:2014-02-27 16:39:02

标签: asp.net viewstate

我试图通过使用按钮一次更新整个列表,但在后面的代码中,我丢失了列表项的数据,它返回null,尽管它有项目。请以这种方式帮助我。我的代码在下面

ASPX

<asp:ListView ID="ProjectsList" runat="server" DataKeyNames="ProjectId" DataSourceID="EntityDataSource1" ViewStateMode="Enabled"> 

    <AlternatingItemTemplate>
        <tr style="background-color: #FFF8DC;">
            <td><asp:Label ID="ProjectIdLabel" runat="server" Text='<%# Eval("ProjectId") %>' /></td>
            <td><asp:Label ID="SchoolIdLabel" runat="server" Text='<%# Eval("SchoolId") %>' /></td>
            <td><asp:Label ID="FairIdLabel" runat="server" Text='<%# Eval("FairId") %>' /></td>
            <td><asp:Label ID="CategoryIdLabel" runat="server" Text='<%# Eval("CategoryId") %>' /></td>
            <td><asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></td>
            <td><asp:Label ID="BalanceLabel" runat="server" Text='<%# Eval("Balance") %>' /></td>
            <td><asp:CheckBox ID="FinalistCheckBox" runat="server" Checked='<%# Eval("Finalist") %>' Enabled="true" /></td>
            <td><asp:Label ID="ProjectStatusLabel" runat="server" Text='<%# Eval("ProjectStatus") %>' /></td>
        </tr>
    </AlternatingItemTemplate>

    <ItemTemplate>
        <tr style="background-color: #DCDCDC; color: #000000;">
            <td><asp:Label ID="ProjectIdLabel" runat="server" Text='<%# Eval("ProjectId") %>' /></td>
            <td>asp:Label ID="SchoolIdLabel" runat="server" Text='<%# Eval("SchoolId") %>' /></td>
            <td><asp:Label ID="FairIdLabel" runat="server" Text='<%# Eval("FairId") %>' /></td>
            <td><asp:Label ID="CategoryIdLabel" runat="server" Text='<%# Eval("CategoryId") %>' /></td>
            <td><asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></td>
            <td><asp:Label ID="BalanceLabel" runat="server" Text='<%# Eval("Balance") %>' /></td>
            <td><asp:CheckBox ID="FinalistCheckBox" runat="server" Checked='<%# Eval("Finalist") %>' Enabled="true" /></td>
            <td><asp:Label ID="ProjectStatusLabel" runat="server" Text='<%# Eval("ProjectStatus") %>' /></td>
        </tr>
    </ItemTemplate>

    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
                        <tr runat="server" style="background-color: #DCDCDC; color: #000000;">
                            <th runat="server">ProjectId</th>
                            <th id="Th1" runat="server">SchoolId</th>
                            <th id="Th2" runat="server">FairId</th>
                            <th id="Th3" runat="server">CategoryId</th>
                            <th runat="server">Title</th>
                            <th id="Th4" runat="server">Balance</th>
                            <th runat="server">Finalist</th>
                            <th runat="server">ProjectStatus</th>
                        </tr>
                        <tr id="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;"></td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:ListView>

<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=MyEntity" DefaultContainerName="MyEntity" EnableFlattening="False" EnableUpdate="True" EntitySetName="Projects">
</asp:EntityDataSource>
<asp:Button ID="FinalistButton" runat="server" Text="Finalist" OnClick="FinalistButton_Click" CssClass="imageButtonStyle" />

和我的aspx.cs ......

protected void FinalistButton_Click(object sender, EventArgs e)
{
    //here I can have all the items but there is no value
    foreach (ListViewItem item in ProjectsList.Items) 
    {
        if (item.ItemType == ListViewItemType.DataItem)
        {
            CheckBox final = (CheckBox)item.FindControl("Finalist");
            TextBox proidText = (TextBox)item.FindControl("ProjectId");
            int proId = Convert.ToInt32(proidText.Text);
            if (final.Checked)  
            {
                //some code
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

要插入或更新数据,模板&lt; InsertTemplate则&GT;和/或&lt; EditItemTemplate模板&GT;应该包括在内,也许他们会失踪。模板&lt;的ItemTemplate&GT;和&lt; AlternateItemTemplate&GT;只是用来显示数据。要更改模板,请添加一些命令按钮。而你使用Bind而不是Eval

您在以下链接中进行了演练,但不是很好。也许你可以找到一个更好的例子。

http://msdn.microsoft.com/en-us/library/bb515103.aspx