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!
listview-help.bmp
lkimble22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
you can do a bulk edit:
http://www.codeproject.com/KB/webforms/BulkEditGridView.aspx

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.
0
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?

0
Kyle AbrahamsSenior .Net DeveloperCommented:
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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

http://mattberseth.com/blog/2008/05/bulk_inserting_data_with_the_l.html

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."
  Source="WebApplication3"
  StackTrace:
       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)
  InnerException:
0
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your code and highlite the line that triggers the error?
0
lkimble22Author Commented:
ged325,
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);


...
<script>
        //*****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";
                    conn.Open();
                    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());
                        dCmd.ExecuteNonQuery();
                    }
                    conn.Close();
                }
            }
                lv.EditIndex = -1;
                // Rebind the details
                BindPersonDetails();
        }
...        //****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)
        {            
            base.OnItemCommand(e);
            SaveItems();
        }
 
        /// <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);
        }...
</script>
...
<%--Listview without an edititemtempate or datasource--%>
        <asp:ListView ID="lv" runat="server">
            <ItemTemplate>
                <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 />
                </td>
            </ItemTemplate>        
            <LayoutTemplate>
                <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">
                        </td>
                    </tr>
                </table>
                <div style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
                </div>
            </LayoutTemplate>
        </asp:ListView>
...

Open in new window

0
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:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.updateitem.aspx
did you specify an update command?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
also it looks like they are using item.DisplayIndex . . . you may be running out of bounds.  
0
Kyle AbrahamsSenior .Net DeveloperCommented:
do you need any more help with this?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.