ListView + Linq without LinqDataSource: Editing!

Last post 01-25-2008 12:13 PM by GabrielX. 2 replies.

Sort Posts:

  • ListView + Linq without LinqDataSource: Editing!

    01-23-2008, 5:01 PM
    • Loading...
    • GabrielX
    • Joined on 01-21-2008, 10:36 PM
    • Posts 5

    Hi there!

    I'm trying to use a ListView to display a list attached to a Linq datasource. It looks something like this: 

    1          var ivs = from i in a.Activities
    2                    where i.DueDate >= DateTime.Now
    3                    select new { i.ActivityID, i.DueDate, i.Note };
    4          UpcomingInterviewList.DataSource = upcominginterviews;
    5          UpcomingInterviewList.DataBind();
    6    
    
    When I click on the edit button, I got an error saying that the ItemEditing event wasn't handled. So, I handled it (didn't do much, just got the item that was editing, but didn't specifically /do/ anything with it), and it seems like the ListView isn't properly going into "edit" mode. I can't find anything in the MS documentation about this, and everyone else on earth seems to be using LinqDataSource components, which I've been avoiding because I like being a little closer to the metal.

    What's going on? What am I supposed to do in the ItemEditing event to make the magic happen?

    Thanks in advance!

    gX

    Filed under: , ,
  • Re: ListView + Linq without LinqDataSource: Editing!

    01-25-2008, 12:10 AM
    Answer

    Hi GabrielX ,

    GabrielX:

    What's going on? What am I supposed to do in the ItemEditing event to make the magic happen?

    GabrielX:
    and it seems like the ListView isn't properly going into "edit" mode.

    Here is my sample,

    <asp:ListView ID="lvItems" runat="server"   ItemPlaceholderID="layoutTemplate"
                DataKeyNames="Pk" InsertItemPosition="LastItem" 
                onitemediting="lvItems_ItemEditing" onitemupdating="lvItems_ItemUpdating" onselectedindexchanged="lvItems_SelectedIndexChanged" 
                >
                <LayoutTemplate>
                    <div id="layoutTemplate" runat="server" />
                </LayoutTemplate>
                <ItemTemplate>
                    <div class="itemdisplay">
                        <b>
                            <%# Eval("Sku") %></b><br />
                        <%# Eval("Abstract") %></div>
                    <asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
                    <asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <div class="itemdisplayalternate">
                        <b>
                            <%# Eval("Sku") %></b><br />
                        <%# Eval("Abstract") %></div>
                    <asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
                    <asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
                </AlternatingItemTemplate>
                <EditItemTemplate>
                    <div class="gridalternate">
                        Sku:
                        <asp:TextBox runat="server" ID="txtSku" Text='<%# Bind("Sku") %>'></asp:TextBox>
                        <br />
                        Abstract:
                        <asp:TextBox runat="server" ID="txtAbstract" Text='<%# Bind("Abstract") %>'></asp:TextBox>
                        <br />
                        <asp:Button ID="Button3" runat="server" CommandName="Update" Text="Update" />
                        <asp:Button ID="Button4" runat="server" CommandName="Cancel" Text="Cancel" /><br />
                    </div>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <div style="background: Yellow">
                        <asp:TextBox runat="server" ID="txtSku" Text='<%# Bind("Sku") %>'></asp:TextBox>
                        <br />
                        <asp:TextBox runat="server" ID="txtAbstract" Text='<%# Bind("Abstract") %>'></asp:TextBox>
                        <br />
                    </div>
                    <asp:Button ID="Button3" runat="server" CommandName="Insert" Text="Insert" />
                    <asp:Button ID="Button4" runat="server" CommandName="Cancel" Text="Cancel" /><br />
                </InsertItemTemplate>
            </asp:ListView>
      
    Table1DataContext context = new Table1DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NORTHWND.MDF;Integrated Security=True;User Instance=True");
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    bind();
            }
    
            private void bind()
            {
                var items = from item in context.Table1s
                            select new { item.PK, item.Abstract, item.Sku };
                this.lvItems.DataSource = items;
                lvItems.DataBind();
            }
    
            protected void lvItems_ItemEditing(object sender, ListViewEditEventArgs e)
            {
                this.lvItems.EditIndex = e.NewEditIndex;
                bind();
                
            }
    
            protected void lvItems_ItemUpdating(object sender, ListViewUpdateEventArgs e)
            {
                TextBox sku = lvItems.Items[e.ItemIndex].FindControl("txtSku") as TextBox;
                TextBox abstrac = lvItems.Items[e.ItemIndex].FindControl("txtAbstract") as TextBox;
    
                var items = from item in context.Table1s
                            where item.PK.ToString() == lvItems.DataKeys[e.ItemIndex].Value.ToString()
                            select item;
                foreach (var i in items)
                {
                    i.Sku = sku.Text;
                    i.Abstract = abstrac.Text;
                }
                context.SubmitChanges();
                this.lvItems.EditIndex = -1;
                bind();
    
            }
    
            protected void lvItems_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
     
    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: ListView + Linq without LinqDataSource: Editing!

    01-25-2008, 12:13 PM
    • Loading...
    • GabrielX
    • Joined on 01-21-2008, 10:36 PM
    • Posts 5

    Thanks! This answers my question, and now I see that using a ListView isn't going to work very well for my purposes.

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter