Link to home
Start Free TrialLog in
Avatar of lkimble22
lkimble22

asked on

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
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

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.
Avatar of lkimble22
lkimble22

ASKER

Thanks for such a quick response...

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

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:
can you post your code and highlite the line that triggers the error?
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

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?
also it looks like they are using item.DisplayIndex . . . you may be running out of bounds.  
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial