ListView ItemInserted not called

Status
Not open for further replies.

patrick

Well-known member
Joined
Dec 5, 2021
Messages
305
Programming Experience
1-3
I have attached the source code.

Insert function does not work.

Attached File : WebApplication12.zip


aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication11
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public class UserInfo
        {
            public UserInfo(string sno, string sname)
            {
                No = sno;
                Name = sname;
            }
            private string a;
            public string No
            {
                get { return a; }
                set { a = value; }
            }
            private string b;
            public string Name
            {
                get { return b; }
                set { b = value; }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ContactsListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
        {
            // This event is never called :(
        }

        protected void ListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            UserInfo test = new UserInfo("66", "BALKC");
            ListViewDataItem newItem = new ListViewDataItem(ListView1.Items.Count, ListView1.Items.Count);
            newItem.DataItem = test;
            ListView1.Items.Insert(ListView1.Items.Count, newItem);
            ListView1.Items.Add(newItem);
        }

        protected void ListView_Load(object sender, EventArgs e)
        {
            if (ListView1.Items.Count <= 0)
            {
                List<UserInfo> listUsers = new List<UserInfo>();

                listUsers.Add(new UserInfo("1", "Strawberry"));
                listUsers.Add(new UserInfo("30", "Banana"));

                ListView1.DataSource = listUsers;
                ListView1.DataBind();
            }
            else
            {
                InsertList();
            }

            GetListViewItems();
        }

        public void GetListViewItems()
        {
            foreach (ListViewDataItem item in ListView1.Items)
            {
                ListViewDataItem Item = item;
                Label ddl = ((Label)(Item.FindControl("NoLabel")));
            }
        }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            ListView1.SelectedIndex = e.NewSelectedIndex;

            string pid = ListView1.SelectedDataKey.Value.ToString();

            //string pid = ListView1.DataKeys[e.NewSelectedIndex].Value.ToString();

            //Label1.Text = "Selected Product ID: " + pid;

            //BindData();

            string sNo = "Selected Product ID: " + pid;
            string sss = "Aaa";
        }

        protected void InsertList()
        {
            UserInfo test = new UserInfo("55", "JACK");
            ListViewDataItem newItem = new ListViewDataItem(ListView1.Items.Count, ListView1.Items.Count);
            newItem.DataItem = test;
            ListView1.Items.Insert(ListView1.Items.Count, newItem);
            ListView1.Items.Add(newItem);
        }

    }
}


aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:ListView ID="ListView1" runat="server" DataKeyNames="No" OnItemInserted="ContactsListView_ItemInserted"
                InsertItemPosition="LastItem" OnItemInserting="ListView_ItemInserting"
                OnLoad="ListView_Load" OnSelectedIndexChanging="ListView1_SelectedIndexChanging" ViewStateMode="Enabled">

                <LayoutTemplate>
                    <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
                        <tr runat="server" id="itemPlaceholder" />
                    </table>
                    <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
                        <Fields>
                            <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowLastPageButton="True"
                                FirstPageImageUrl="~/Images/PgFirst.gif" LastPageImageUrl="~/Images/PgLast.gif"
                                PreviousPageImageUrl="~/Images/PgPrev.gif" NextPageImageUrl="~/Images/PgNext.gif" />
                        </Fields>
                    </asp:DataPager>
                </LayoutTemplate>

                <ItemTemplate>
                    <tr id="Tr3" runat="server">
                        <td valign="top">
                            <asp:LinkButton ID="SelectButton" runat="server" Text="..." CommandName="Select" />
                        </td>
                        <td>&nbsp;
                            <asp:Label ID="NoLabel" runat="server" Text='<%#Eval("No") %>' />
                        </td>
                        <td>&nbsp;
                            <asp:Label ID="NameLable" runat="server" Text='<%#Eval("Name") %>' />
                        </td>

                    </tr>
                </ItemTemplate>

                <InsertItemTemplate>
                    <tr style="background-color: #D3D3D3">
                        <td valign="top">

                            <asp:Label runat="server" ID="NoLabel" AssociatedControlID="NoTextBox" Text="First Name" />
                            <asp:TextBox ID="NoTextBox" runat="server" Text='<%#Bind("No") %>' /><br />

                            <asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox" Text="Last Name" />
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' /><br />
                          
                        </td>
                        <td>
                            <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                        </td>
                    </tr>
                </InsertItemTemplate>


                <SelectedItemTemplate>
                    <tr style="background-color: #336699; color: White;">
                        <td>
                            <asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" ForeColor="White" />
                        </td>
                        <td><%# Eval("No")%></td>
                        <td><%# Eval("Name")%></td>
                    </tr>
                </SelectedItemTemplate>


            </asp:ListView>

        </div>
    </form>
</body>
</html>
 
Please reply on your other thread about this. This is just a continuation of the same issue of you learning how to insert items into a listview. I am just locking this thread instead of trying to merge again.
 
Status
Not open for further replies.

Latest posts

Back
Top Bottom