Solved

Listview Binding, Editing/Updating, Insert etc. from code behind

Posted on 2011-03-24
2
892 Views
Last Modified: 2012-05-11
I have a listview that is bound in code behind.
The listview data source has a lot of datetime fields and I need to apply formatting before saving to the database, so I thought it would be better if I controlled edits, inserts etc. all in code behind.

How do I manage the listview in code behind?

Thanks

Protected Sub FillSchedules()

      Dim dStart As Date = Date.Parse(Me.txtCalendar.Text)
      Dim d As Date = dStart.AddDays(Me.ddDays.SelectedValue - 1)
      Dim sc As New Scheduling

      Dim u As New SalonUtilities
      Dim sUserID As String = u.CheckSessionAndReturnUser

      Dim dc As New SalonDataClassesDataContext

      Dim q = (From p In dc.Schedules Where p.SchedStartDateTime >= dStart AndAlso _
             p.SchedEndDateTime.Date <= d.Date And p.UserID.ToString = sUserID _
            Order By p.SchedStartDateTime Select p)

      Me.lstSchedules.DataSource = q
      Me.lstSchedules.DataBind()
    
      Me.UpdatePanelCalendar.Update()

   End Sub



Private Sub lstSchedules_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles lstSchedules.ItemUpdating
      Dim dc As New SalonDataClassesDataContext
      Dim iSchedID As Integer = e.Keys("SchedID")
      Dim dStart As DateTime = e.NewValues("txtStartTimeUp")
      Dim dEnd As DateTime = e.NewValues("txtEndTimeUp")
      Dim dDate As Date = e.NewValues("txtCalendarUp")
      Dim vtmStart As New DateTime(dDate.Year, dDate.Month, dDate.Day, dStart.Hour, dStart.Minute, 0)
      Dim vtmEnd As New DateTime(dDate.Year, dDate.Month, dDate.Day, dEnd.Hour, dEnd.Minute, 0)
      Dim ddClient As DropDownList = CType(Me.lstSchedules.EditItem.FindControl("ddClientUp"), DropDownList)
      Dim ddServ As DropDownList = CType(Me.lstSchedules.EditItem.FindControl("ddServiceOfferedUp"), DropDownList)

      Dim q = (From p In dc.Schedules Where p.SchedID = iSchedID Select p).FirstOrDefault

      q.CustomerID = ddClient.SelectedValue
      q.LUServiceID = ddServ.SelectedValue
      q.SchedStartDateTime = vtmStart
      q.SchedStart = vtmStart
      q.SchedEndDateTime = vtmEnd
      q.SchedEnd = vtmEnd
      q.SchedDate = vtmStart
      q.DateSchedMade = Now


      dc.SubmitChanges()
   End Sub


  <asp:ListView ID="lstSchedules"  runat="server"  
         DataKeyNames="SchedID"     
         InsertItemPosition="LastItem"  >
         <LayoutTemplate>
             <div style="width:100%">
                 <table ID="itemPlaceholderContainer" runat="server" cellpadding="2" 
                     style="vertical-align:top" class="ListViewTable">
                     <tr ID="Tr1" runat="server">
                         <td class="ListViewHeader" style="width:8%">
                             <asp:Label ID="Label17" runat="server" Text="Delete"></asp:Label>
                         </td>
                         
                         <td class="ListViewHeader" style="width:14%">
                             <asp:Label ID="lblDate" runat="server" Text="Schedule Date"></asp:Label>
                             
                         </td>
                         
                         <td class="ListViewHeader" style="width:18%">
                             <asp:Label ID="lblClient" runat="server" Text="Client"></asp:Label>
                         </td>
                         <td class="ListViewHeader" style="width:15%">
                             <asp:Label ID="lblService" runat="server" Text="Service"></asp:Label>
                             
                         </td>
                         <td class="ListViewHeader" style="width:10%">
                             <asp:Label ID="lblStart" runat="server" Text="Start time"></asp:Label>
                             
                         </td>
                         <td class="ListViewHeader" style="width:10%">
                             <asp:Label ID="lblEnd" runat="server" Text="End Time"></asp:Label>
                             
                         </td>
                         
                         <td class="ListViewHeader" style="width:10%">
                             <asp:Label ID="Label18" runat="server" Text="Actions"></asp:Label>
                         </td>
                         
                          <td class="ListViewHeader" style="width:12%">
                             <asp:Label ID="Label16" runat="server" Text="Add New"></asp:Label>
                             
                         </td>
                     </tr>
                     <tr ID="itemPlaceholder" runat="server">
                     </tr>
                 </table>
             </div>
             </td>
         </LayoutTemplate>                              
         <ItemTemplate>
             <tr style="">
                 
                 <td>
                     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete">Delete</asp:LinkButton>
                 </td>
                 <td>
                     <asp:Label ID="lblSched" runat="server" 
                        Text='<%#Eval("SchedStartDateTime", "{0:d}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblClient" runat="server" Text='<%#Eval("Client.FullName")%>' />
                       
                 </td>
                 <td>
                     <asp:Label ID="lblService" runat="server" Text=' <%#Eval("ServicesLU.ServiceOffered")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblStart" runat="server" Text=' <%#Eval("SchedStart", "{0:t}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblEnd" runat="server" Text=' <%#Eval("SchedEnd", "{0:t}")%>' />
                 </td>
                 
               
                 <td align="center">
                     <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                 </td>
                 <td>
                     <asp:LinkButton ID="LnkCreateService" runat="server" OnClick="CreateService" 
                        CommandName="CreateService">Create Service</asp:LinkButton>
                 </td>
             </tr>
         </ItemTemplate>
         <AlternatingItemTemplate>
         
             <tr style="background-color:#f3d4fd" >
                 <td>
                     <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
                 </td>
                 <td>
                     <asp:Label ID="lblSched" runat="server" 
                         Text='<%#Eval("SchedStartDateTime", "{0:d}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblClient" runat="server" 
                         Text='<%#Eval("Client.FullName")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblService" runat="server" Text=' <%#Eval("ServicesLU.ServiceOffered")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblStart" runat="server" Text=' <%#Eval("SchedStart", "{0:t}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblEnd" runat="server" Text=' <%#Eval("SchedEnd", "{0:t}")%>' />
                 </td>
                 
                 <td align="center">
                     <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                 </td>
                 <td>
                     <asp:LinkButton ID="LnkCreateService" runat="server"  OnClick="CreateService" 
                        CommandName="CreateService">Create Service</asp:LinkButton>
                 </td>
             </tr>
         </AlternatingItemTemplate>
         <EmptyDataTemplate>
             <table ID="Table1" runat="server" style="">
                 <tr>
                     <td>
                         <asp:Label ID="Label14" runat="server" Text=" No schedules assigned for the day."></asp:Label>
                        </td>
                 </tr>
             </table>
         </EmptyDataTemplate>
         <SelectedItemTemplate>
             <tr style="">
                 <td>
                     <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
                 </td>
                 
                 <td>
                     <asp:Label ID="lblSched" runat="server" 
                        Text='<%#Eval("SchedStartDateTime", "{0:d}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblClient" runat="server" Text='<%#Eval("Client.FullName")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblService" runat="server" Text=' <%#Eval("ServicesLU.ServiceOffered")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblStart" runat="server" Text=' <%#Eval("SchedStart", "{0:t}")%>' />
                 </td>
                 <td>
                     <asp:Label ID="lblEnd" runat="server" Text=' <%#Eval("SchedEnd", "{0:t}")%>' />
                 </td>
                 
                 
                 <td align="center">
                     <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                 </td>
                 <td>
                     <asp:LinkButton ID="LnkCreateService" runat="server"  OnClick="CreateService" 
                        CommandName="CreateService">Create Service</asp:LinkButton>
                 </td>
             </tr>
         </SelectedItemTemplate>
         <InsertItemTemplate>
             <tr style="">
                 <td >
                     &nbsp;
                  </td>
                 
                 <td valign="top">
                     <asp:TextBox ID="txtCalendarIns" runat="server" 
                        Text='<%# Bind("SchedStartDateTime") %>' Width="75%"></asp:TextBox>
                     <img id="ClickCalIns" alt="" src="../images2020/Calendar2.jpg" 
                        style="vertical-align:bottom" />
                     <asp:CalendarExtender ID="CalendarExtender1" runat="server" Enabled="True" 
                        PopupButtonID="ClickCalIns" TargetControlID="txtCalendarIns">
                     </asp:CalendarExtender>

                 </td>                                      
                
                 
                 
                 <td valign="top">
                       <asp:DropDownList ID="ddClientIns" runat="server" AppendDataBoundItems="true" 
                          DataSourceID="lnqClientSchedule" DataTextField="FullName" 
                          DataValueField="CustomerID" Width="95%">
                          <asp:ListItem Selected="True" Value="-1">
                           --- Select Client ---
                        </asp:ListItem>
                       </asp:DropDownList>
                 </td>
                 
                 
                 
                 <td valign="top">
                     <asp:DropDownList ID="ddServiceOfferedIns" runat="server"  Width="95%"
                        DataSourceID="lnqServiceLUSchedule" DataTextField="ServiceOffered" 
                        DataValueField="LUServiceID">
                        <asp:ListItem Selected="True" Value="-1">
                           --- Select Service ---
                        </asp:ListItem>
                     </asp:DropDownList>
                 </td>

                 
                 
                 <td valign="top">
                      <asp:TextBox ID="txtStartTimeIns" runat="server" 
                         Text='<%# Bind("SchedStart") %>' Width="95%"></asp:TextBox>
                 </td>

                 
                 
                 <td valign="top">
                     <asp:TextBox ID="txtEndTimeIns" runat="server" Text='<%# Bind("SchedEnd") %>' 
                        Width="95%"></asp:TextBox>
                 </td>

                 
                 
                 <td valign="top" align="center">
                       <asp:LinkButton ID="LnkInsertButton" runat="server" causesvalidation="true" 
                          CommandName="Insert">Insert</asp:LinkButton>
                       <asp:LinkButton ID="LnkCancelButton" runat="server" CommandName="Cancel">Clear</asp:LinkButton>
                 </td>

                 
                 
                 <td valign="top">
                    <asp:Label ID="Label15" runat="server" Text=""></asp:Label>
                                                           
                 </td>

                 
             </tr>
         </InsertItemTemplate>

         <EditItemTemplate>
             <tr style="">
                 <td >
                     &nbsp;
                  </td>
                <td valign="top">
                       <asp:TextBox ID="txtCalendarUp" Width="75%" runat="server" Text='<%# Bind("SchedStartDateTime") %>' ></asp:TextBox>
                      <img style="vertical-align:bottom" alt="" id="ClickCalUp" src="../images2020/Calendar2.jpg" />
                     <asp:CalendarExtender ID="CalendarExtender1" TargetControlID="txtCalendarUp"
                      runat="server" PopupButtonID="ClickCalUp" Enabled="True" ></asp:CalendarExtender>
                 </td>
                 
                 <td valign="top">
                     <asp:DropDownList ID="ddClientUp" runat="server" DataSourceID="lnqClientSchedule"
                      DataTextField="FullName" DataValueField="CustomerID" AppendDataBoundItems="true" >
                           <asp:ListItem Value="-1" Selected="True">
                           --- Select Client ---
                        </asp:ListItem>
                     </asp:DropDownList>

                 </td>                                      
                 <td valign="top">
                      <asp:DropDownList ID="ddServiceOfferedUp" runat="server"  DataSourceID="lnqServiceLUSchedule"
                        DataTextField="ServiceOffered" DataValueField="LUServiceID" >
                            <asp:ListItem Value="-1" Selected="True">
                           --- Select Service ---
                        </asp:ListItem>
        
                         </asp:DropDownList>
                 </td>
                 <td valign="top">
                     <asp:TextBox ID="txtStartTimeUp" Width="95%" runat="server" Text='<%# Bind("SchedStart") %>' ></asp:TextBox>
                 </td>
                 <td valign="top">
                       <asp:TextBox ID="txtEndTimeUp" Width="95%" runat="server" Text='<%# Bind("SchedEnd") %>' ></asp:TextBox>
                 </td>
                 <td align="center" valign="top">
                     <asp:LinkButton ID="LnkUpdateButton" runat="server"  causesvalidation="true" 
                        CommandName="Update">Update</asp:LinkButton>
                     <asp:LinkButton ID="LnkCancelButton" runat="server" CommandName="Cancel">Clear</asp:LinkButton>
                 </td>
                 <td>
                  <asp:Label ID="Label15" runat="server" Text=""></asp:Label> 
                 
                 </td>
             </tr>
         </EditItemTemplate>

     </asp:ListView>
      <asp:LinqDataSource ID="lnqServiceLUSchedule" runat="server" 
         ContextTypeName="Service2020.SalonDataClassesDataContext" 
         OrderBy="ServiceOffered" TableName="ServicesLUs" Where="UserID == @UserID">
         <WhereParameters>
            <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
         </WhereParameters>
      </asp:LinqDataSource>
      <asp:LinqDataSource ID="lnqClientSchedule" runat="server" 
         ContextTypeName="Service2020.SalonDataClassesDataContext" OrderBy="FullName" 
         TableName="vClientNames" Where="UserID == @UserID">
         <WhereParameters>
            <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />
         </WhereParameters>
      </asp:LinqDataSource>

Open in new window

0
Comment
Question by:Sheritlw
2 Comments
 
LVL 13

Accepted Solution

by:
agarwalrahul earned 500 total points
ID: 35212679
0
 

Author Comment

by:Sheritlw
ID: 35214743
Thank you aqarwalrahul!

I have been looking for MONTHS for an example like that.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question