简体   繁体   English

带有状态的ASP.net ListView

[英]ASP.net listview with state

I have a dynamic list in ASP.net front end. 我在ASP.net前端有一个动态列表。 User can click a button and add as many entries as it wants. 用户可以单击一个按钮并根据需要添加任意多个条目。

I'm using ViewState to save this data: 我正在使用ViewState保存此数据:

if(ViewState["items"] != null)
{
    ListItems.DataSource = ViewState["items"];
}          
ListItems.DataBind();

And there is a callback as follows: 并且有一个如下的回调:

protected void AddItemClick(object sender, EventArgs e)
{
    List<UserEntry> ue;
    if (ViewState["items"] == null)
    {
        ue = new List<UserEntry>();
    }
    else
    {
        ue = (List<UserEntry>)ViewState["items"];
    }
    ue.Add(new UserEntry());
    ViewState["items"] = ue;
}

It's working fine, the problem is that, every time I add a new item, I loose any data I've entered in the other rows. 一切正常,问题在于,每次添加新项目时,我都会丢失在其他行中输入的所有数据。 How can I keep this information? 如何保存此信息?

Edit: I'm calling it from the .aspx page: 编辑:我从.aspx页调用它:

<asp:ListView ID="ListItems" class="block" SortExpression="DataType" ItemStyle-Wrap="false"  runat="server">
    <ItemTemplate  >
        <table>
            <tr>
                <asp:TextBox ID="Name" runat="server" class="inline" Text='<%# Eval("text") %>'></asp:TextBox>

            </tr>
        </table>
    </ItemTemplate>
</asp:ListView>

Thanks in advance 提前致谢

Where are you calling this method? 您在哪里调用此方法? Are you calling in the same page? 您在同一页面中打电话吗? Before you construct the List you might be assigning to ListItems control. 在构造列表之前,您可能会分配给ListItems控件。

protected void AddItemClick(object sender, EventArgs e)
{
    List<UserEntry> ue;
    if (ViewState["items"] == null)
    {
        ue = new List<UserEntry>();
    }
    else
    {
        ue = (List<UserEntry>)ViewState["items"];
    }
    ue.Add(new UserEntry());
    ViewState["items"] = ue;
    ListItems.DataSource = ue;
    ListItems.DataBind();
}

In this line, you can assign the collection to List and bind while adding in the ViewState. 在此行中,可以在添加ViewState的同时将集合分配给List并进行绑定。

There could be several points to check: 可能有几点要检查:

  1. If you are assigning the values to ViewState on Page_Load then you might not be checking if it is a postback or not to overcome it you could just simply do the values assignment part to ListItems in an if condition: if(!Page.IsPostback){/* do stuff */ } 如果要在Page_Load上将值分配给ViewState ,则可能不检查它是否是postback或者不克服它,您可以简单地在if条件下对ListItems进行值分配: if(!Page.IsPostback){/* do stuff */ }
  2. You might want to bind the ListItems each time you modify your list 您可能希望每次修改列表时都绑定ListItems
  3. You Could simply tweak with your code to see where the issue is nobody can help you investigate your code better than you! 您可以简单地调整代码以查看问题出在哪里,没有人能比您更好地调查代码!
  4. Or finally, you may want to skip the ViewState at all, what you can do is: 或最后,您可能要完全跳过ViewState,您可以做的是:

     protected void AddItemClick(object sender, EventArgs e) { List<UserEntry> ue = (List<UserEntry>)ListItems.DataSource; if(ue == null) { ue = new List<UserEntry>(); } ue.Add(new UserEntry()); ListItems.DataSource = ue; ListItems.DataBind(); } // Just an idea though 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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