• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2786
  • Last Modified:

ASP.NET ListView single click edit

I need to allow direct single-click editing (like in excel without a button or link) of fields in an ASP.NET ListView object (3.5 framework), instead of using the edit button. I would also like information on whether or not it is possible to remove the individual record edits replacing them with a bulk ListView wide update/save button....so only after all changes had been made to all records with a second click be necessary.

I currently have a webform that mimics an excel spreadsheet and the new ListView allowed me to view my records vertically (columns on the left).  Sorry for the heavy censorship, but the attached picture is a sample of the application.  Thanks for all your help!
  • 6
  • 3
1 Solution
Kyle AbrahamsSenior .Net DeveloperCommented:
you can do a bulk edit:

Which should answer the first part of your question regarding a single click.  If you want a view mode/edit mode then you can make the cells onclick event call the edit command.
lkimble22Author Commented:
Thanks for such a quick response...

the example you've shown is for a gridview, will that matter, or are there enough similarities?

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

lkimble22Author Commented:
I found pretty much exactly what I was looking for here:


though I can't seem to get it to work with a datasourceobject targeting a tableadapter from the dataset xsd I'm using.  He implements a class for his own data access layer, but I can't seem to make much sense of why I keep getting the following exception:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
       at WebApplication3._Default.UpdateListViewItem(Object sender, EventArgs e) in C:\Documents and Settings\Loren Kimble\My Documents\Visual Studio 2008\Projects\WebApplication3\WebApplication3\Default.aspx.cs:line 53
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your code and highlite the line that triggers the error?
lkimble22Author Commented:
Thanks for the reply, I've tried several techniques for the update (as seen in code below), but the error consistently occurs during the traversal of listview elements such as:

foreach (ListViewDataItem di in this.Items)
   this.UpdateItem(di.DataItemIndex, false);

        //*****FIRST UPDATING TECHNIQUIE I TRIED***method for updating the data after the update button click
        protected void Updatevw(object sender, EventArgs e)
            foreach (ListViewDataItem i in this.lv.Items)
                ListViewItem item = lv.Items[i.DataItemIndex];
                int bpforecastid = new int();
                int bpforecastid = int.Parse(lv.DataKeys[i.DataItemIndex].Value.ToString());
                TextBox tForecast = (TextBox)item.FindControl("FORECASTLabel");
                TextBox tActual = (TextBox)item.FindControl("ACTUALLabel");
                // insert records into database
                using (SqlConnection conn = new SqlConnection(_connStr))
                    string Sql = "update BP_FORECAST set forecast = @forecast, actual = @actual where BPFORECASTID = @bpforecastid";
                    using (SqlCommand dCmd = new SqlCommand(Sql, conn))
                        dCmd.Parameters.AddWithValue("@bpforecastid", bpforecastid);
                        dCmd.Parameters.AddWithValue("@forecast", tForecast.Text.Trim());
                        dCmd.Parameters.AddWithValue("@Actual", tActual.Text.Trim());
                lv.EditIndex = -1;
                // Rebind the details
...        //****SECOND TECHNIQUE FOR UPDATING DATA*** this time actually wiring up 
           // the objectdatasource to the ListView by datasourceid...still blows up at the 
           //   "this.UpdateItem(di.DataItemIndex, false);"
        protected override void OnItemCommand(ListViewCommandEventArgs e)
        /// <summary>
        /// Save ListView Items.
        /// </summary>
        public void SaveItems()
            // Update all items on Command (InsertItem is not included in Items collection)
            foreach (ListViewDataItem di in this.Items)
                this.UpdateItem(di.DataItemIndex, false);
<%--Listview without an edititemtempate or datasource--%>
        <asp:ListView ID="lv" runat="server">
                <td runat="server" style="background-color: #E0FFFF;color: #333333;">
                    <asp:Label ID="BPFORECASTIDLabel" runat="server" Text='<%# Eval("BPFORECASTID") %>' /><br />
                    <asp:Label ID="BPIDLabel" runat="server" Text='<%# Eval("BPID") %>' /><br />
                    <asp:Label ID="PAGELabel" runat="server" Text='<%# Eval("PAGE") %>' /><br />
                    <asp:Label ID="MONTHIDLabel" runat="server" Text='<%# Eval("MONTHID") %>' /><br />
                    <asp:TextBox ID="FORECASTLabel" runat="server" Text='<%# Bind("FORECAST") %>' /><br />
                    <asp:TextBox ID="ACTUALLabel" runat="server" Text='<%# Bind("ACTUAL") %>' /><br />
                <table runat="server" border="1" 
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                    <tr ID="itemPlaceholderContainer" runat="server">
                        <td ID="itemPlaceholder" runat="server">
                <div style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">

Open in new window

Kyle AbrahamsSenior .Net DeveloperCommented:
Are you using a dataset to do this?  Would be a lot easier for the dataset to do the update then 1 record at a time.  (Dataset would do dirty rows only).

see this:
did you specify an update command?
Kyle AbrahamsSenior .Net DeveloperCommented:
also it looks like they are using item.DisplayIndex . . . you may be running out of bounds.  
Kyle AbrahamsSenior .Net DeveloperCommented:
do you need any more help with this?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now