开发者

Editing in ListView without EditMode on

I have a class

public class MyClass
{
public int id {get; set;}
public string name {get; set;}
}

I store objects of type MyClass in list:

List<MyClass> listOfObjects {get; set;}

I want user to edit data. To display data I am using ListView control - I am binding listOfObject list to ListView:

<asp:ListView runat="server" ID="ListView1">
<LayoutTemplate>    
<table runat="server" id="table1" >       
<tr runat="server" id="itemPlaceholder" >
</tr>     
</table>   
</LayoutT开发者_运维知识库emplate>   
<ItemTemplate>     
<tr id="tr" runat="server">         
<td id="td1" runat="server">             
<asp:TextBox ID="tb1" runat="server" Text='<%#Eval("id") %>' />         
</td>         
<td id="td2" runat="server">             
<asp:TextBox ID="tb2" runat="server" Text='<%#Eval("name")%>' />        
</td>    
</tr>   
</ItemTemplate> 
</asp:ListView>

So far so good, and everything works fine. I see the data in ListView. I do not know how I can edit data in ListView without entering EditMode - I do not have Edit button, I have only textboxes to enter new data. All I want is to bind data edited in textboxes in the opposite direction - into listOfObject list. I have tried many solutions and nothing was working correctly...

I thought that I can read data this way:

var listItems = ListView1.Items.Cast<ListViewDataItem>().Select(a => new MyClass 
           {   
                id = ((TextBox)a.FindControl("tb1")).Text,   
                name = ((TextBox)a.FindControl("tb2")).Text   
           }).ToList<>(); 

but with this I get data which were in textbox before editing...

Help me please, because I have no idea how to make it working (maybe it would be better to use ObjectDataSource?)


Where do you call the last part of your code? I suppose you have a submit button, do you call that code in it's Click EventHandler?

You must be overwriting your data. If you DataBind in the Page's Load event make sure you only do that if the page is not posting back.

protected void Page_Load(object sender, EventArgs e)
        {

             if (!Page.IsPostBack)
            {
                 // databind
             }      
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜