locked
How can I convert my asp listview to being editable? RRS feed

  • Question

  • User1857760563 posted

    Through searching, I've seen posts that indicate you can convert the listview element in ASP to something that can be edited, but I can't seem to get anything to work, so I must be overlooking something obvious and it's time to request help. I have a working listview element but now would like to make some of the fields of a selected row editable.

    Thanks

    HTML:

    <asp:ListView ID="lvwOutput" runat="server" DefaultMode="Edit" CurrentMode="Edit" onitemdatabound="lvwOutput_ItemDataBoundToList" style="height:800px; overflow: scroll" OnItemEditing="lvwOutput_ItemEditing" EditIndex="-1">
    
    <LayoutTemplate>
    
    <table id="lvwOutputTable" border="2" cellpadding="1" style="width=100%; font-family: Calibri; font-size: 12px">
    
    <tr style="background-color:#E5E5FE">
    
    <th align="left"><asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="dateTime">Date Time</asp:LinkButton></th>
    
    <th align="left"><asp:LinkButton ID="LinkButton13" runat="server" CommandName="Sort" CommandArgument="additionalNotes"></asp:LinkButton>Additional Notes</th>
    
    <th></th>
    
    </tr>
    
    <tr id="itemPlaceholder" runat="server"></tr>
    
    </table>
    
    </LayoutTemplate>
    
    <ItemTemplate>
    
    <tr style="width=100%;">
    
    <td><asp:Label runat="server" ID="lblId"><%#Eval("dateTime") %></asp:Label></td>
    
    <td><asp:Label runat="server" ID="Label13"><%#Eval("additionalNotes") %></asp:Label></td> 
    
    </tr>
    
    </ItemTemplate> 
    
    </asp:ListView>
    

    Thursday, June 7, 2018 6:33 PM

Answers

  • User-330142929 posted

    Hi Oerten,

     

    According to your description. I test your codes and make a demo below:

    You should add EditItemTemplate to ListView and process itemediting and ItemUpdating event handler.

    Aspx.

    <asp:ListView ID="ListView1" runat="server" DefaultMode="Edit" CurrentMode="Edit" OnItemDataBound="ListView1_ItemDataBound" OnItemEditing="ListView1_ItemEditing"  OnItemUpdating="ListView1_ItemUpdating" EditIndex="-1">
                    <EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server"></th>
                                            <th runat="server">ID</th>
                                            <th runat="server">Name</th>
                                            <th runat="server">Price</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                </asp:ListView>

    Code behind.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.ListView1.DataSource = GetDataTable();
                    this.ListView1.DataBind(); 
                }
     
            }
            public DataTable GetDataTable()
            {
                SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                SqlCommand command = new SqlCommand();
                command.CommandText = "select * from products";
                command.CommandType = CommandType.Text;
                command.Connection = connection;
                SqlDataAdapter sda = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                
            }
            protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
            {
                this.ListView1.EditIndex = e.NewEditIndex;
                this.ListView1.DataSource = GetDataTable();
                this.ListView1.DataBind();
            }
            protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
            {
                int id = Convert.ToInt32(((Label)ListView1.Items[e.ItemIndex].FindControl("IDLabel1")).Text);
                TextBox name = (TextBox)ListView1.Items[e.ItemIndex].FindControl("NameTextBox");
                TextBox price = (TextBox)ListView1.Items[e.ItemIndex].FindControl("PriceTextBox");
                if (name.Text != string.Empty || price.Text != string.Empty)
                {
                    string Name = name.Text;
                    string Price = price.Text;
                    SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                    SqlCommand command = new SqlCommand();
                    command.CommandText = "update Products Set Name='"+Name+"',Price="+Price+" where Id="+id;
                    
                    command.CommandType = CommandType.Text;
                    command.Connection = connection;
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
                this.ListView1.EditIndex = -1;
                this.ListView1.DataSource = this.GetDataTable();
                this.ListView1.DataBind();
            }
        }

    Table design.

     

    How it works.

    Please feel free to let me know if you have any question.

     

    Best Regards, 

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 8, 2018 9:26 AM

All replies

  • User632428103 posted

    Hello turbo

    you can do that with the 

    <EditItemTemplate>

    in design view looks at this sample

    https://www.aspsnippets.com/Articles/ASPNet-ListView-EditItemTemplate-example-in-C-and-VBNet.aspx

    Friday, June 8, 2018 7:40 AM
  • User-330142929 posted

    Hi Oerten,

     

    According to your description. I test your codes and make a demo below:

    You should add EditItemTemplate to ListView and process itemediting and ItemUpdating event handler.

    Aspx.

    <asp:ListView ID="ListView1" runat="server" DefaultMode="Edit" CurrentMode="Edit" OnItemDataBound="ListView1_ItemDataBound" OnItemEditing="ListView1_ItemEditing"  OnItemUpdating="ListView1_ItemUpdating" EditIndex="-1">
                    <EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server"></th>
                                            <th runat="server">ID</th>
                                            <th runat="server">Name</th>
                                            <th runat="server">Price</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                </asp:ListView>

    Code behind.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    this.ListView1.DataSource = GetDataTable();
                    this.ListView1.DataBind(); 
                }
     
            }
            public DataTable GetDataTable()
            {
                SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                SqlCommand command = new SqlCommand();
                command.CommandText = "select * from products";
                command.CommandType = CommandType.Text;
                command.Connection = connection;
                SqlDataAdapter sda = new SqlDataAdapter(command);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
            protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
            {
                
            }
            protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
            {
                this.ListView1.EditIndex = e.NewEditIndex;
                this.ListView1.DataSource = GetDataTable();
                this.ListView1.DataBind();
            }
            protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
            {
                int id = Convert.ToInt32(((Label)ListView1.Items[e.ItemIndex].FindControl("IDLabel1")).Text);
                TextBox name = (TextBox)ListView1.Items[e.ItemIndex].FindControl("NameTextBox");
                TextBox price = (TextBox)ListView1.Items[e.ItemIndex].FindControl("PriceTextBox");
                if (name.Text != string.Empty || price.Text != string.Empty)
                {
                    string Name = name.Text;
                    string Price = price.Text;
                    SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                    SqlCommand command = new SqlCommand();
                    command.CommandText = "update Products Set Name='"+Name+"',Price="+Price+" where Id="+id;
                    
                    command.CommandType = CommandType.Text;
                    command.Connection = connection;
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
                this.ListView1.EditIndex = -1;
                this.ListView1.DataSource = this.GetDataTable();
                this.ListView1.DataBind();
            }
        }

    Table design.

     

    How it works.

    Please feel free to let me know if you have any question.

     

    Best Regards, 

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 8, 2018 9:26 AM
  • User1857760563 posted

    I tried to copy the code you provided (thank you for that thoroughness) into a webforms document in Visual Studio as an example/demo project but it isn't displaying? I figured I could see exactly how it worked this way to understand it fully. Can you tell me what I'm doing wrong here?

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="listviewEditExample2._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <div class="jumbotron">
            <h1>ASP.NET</h1>
            <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
            <p><a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
        </div>
    
        <div class="row">
            <div class="col-md-4">
                <h2>Getting started</h2>
                <p>
                    ASP.NET Web Forms lets you build dynamic websites using a familiar drag-and-drop, event-driven model.
                A design surface and hundreds of controls and components let you rapidly build sophisticated, powerful UI-driven sites with data access.
                </p>
                <p>
                    <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301948">Learn more &raquo;</a>
                </p>
            </div>
            <div class="col-md-4">
                <h2>Get more libraries</h2>
                <p>
                    NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
                </p>
                <p>
                    <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301949">Learn more &raquo;</a>
                </p>
            </div>
            <div class="col-md-4">
                <h2>Web Hosting</h2>
                <p>
                    You can easily find a web hosting company that offers the right mix of features and price for your applications.
                </p>
                <p>
                    <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301950">Learn more &raquo;</a>
                </p>
            </div>
        </div>
    
    
        <asp:ListView ID="ListView1" runat="server" DefaultMode="Edit" CurrentMode="Edit" OnItemDataBound="ListView1_ItemDataBound" OnItemEditing="ListView1_ItemEditing"  OnItemUpdating="ListView1_ItemUpdating" EditIndex="-1">
                    <EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                            <th runat="server"></th>
                                            <th runat="server">ID</th>
                                            <th runat="server">Name</th>
                                            <th runat="server">Price</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style=""></td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                </asp:ListView>
    </asp:Content>
    

    Friday, June 8, 2018 2:53 PM
  • User-330142929 posted

    Hello turbo,

    According to your description, you should add the Code-behind Code in Default.aspx.cs, and then ensure that you have a sql server table. correct connection string. I suggest you could try the complete code.

    if you want the project file please let me know.

    Best Regards,

    Abraham.

    Friday, June 8, 2018 3:54 PM
  • User1857760563 posted

    Hello Abraham,

    I also added the rest of your code to my empty project, only I did not paste that because the issue I'm experiencing is with the HTML portion (so I only copy pasted the html code)

    If you could provide the entire project, that would be appreciated!

    Friday, June 8, 2018 3:57 PM
  • User1857760563 posted

    Also one more question ...

    How do I reconcile the updated/edited html data table data with the sql backend data? Do I have to index the sql data with a unique identifier so my code knows which row to update or does sql already do this for you?

    Friday, June 8, 2018 4:15 PM
  • User1857760563 posted

    Hello turbo,

    Hi Abraham,

    Did you see my previous replies? Thanks

    According to your description, you should add the Code-behind Code in Default.aspx.cs, and then ensure that you have a sql server table. correct connection string. I suggest you could try the complete code.

    if you want the project file please let me know.

    Best Regards,

    Abraham.

    Sunday, June 10, 2018 3:16 AM
  • User-330142929 posted

    Hi Turtbo,

    I am sorry for the late response.

    How do I reconcile the updated/edited html data table data with the sql backend data? Do I have to index the sql data with a unique identifier so my code knows which row to update or does sql already do this for you?

    We'd better create a unique indentifier column to ensure that the records in the data table column are unique. In fact, we only need to ensure that the SQL statement of the SQL Command object can be executed correctly. We could firstly extract the value of the commandtext property in the SQLServer Manager studio side of the test.

    Here Is my project file link.

    https://microsoftapc-my.sharepoint.com/:u:/g/personal/v-abqian_microsoft_com/EWfggWucP7BGi_QAbQxDuyUBydXLYJbGwbWJ-xHqMkDJIg?e=MdZdrC

    Please note that you need to change the sqlserver database connection string in your project, and make sure that you have the Products table. Here is its table structure design.

    if the link is invalid, please feel free to let me know.

     

    Best Regards,

    Abraham

    Monday, June 11, 2018 1:46 PM
  • User1857760563 posted

    Good morning. Thanks for replying.

    It doesn't allow me to sign into microsoftapc-my.sharepoint.com ? I have a microsoft account, but it tells me that I don't have a microsoftapc-my.sharepoint account?

    Monday, June 11, 2018 2:04 PM
  • User-330142929 posted

    Hi Turbo,

    I have already sent the link to you by private messages. Welcome any questions.

    Tuesday, June 12, 2018 1:37 AM
  • User1857760563 posted

    When I open the project in VS2015, I receive this when I f5 debug it?

    So I deleted the .vs folder and now get "HTTP Error 403.14 - Forbidden"

    I'm using a standard version of VS2015?
    Thursday, June 14, 2018 3:44 PM
  • User-330142929 posted

    Hi turbo,

     

    sorry to tell you that my project is based on VS2017.

    Please try to open this project using VS2017.

    Besides, I suggest you could use VS2015 to rebuild the ListView project for practice purposes.

     

    Best Regards,

    Abraham

    Tuesday, June 19, 2018 1:51 AM