Solved

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

Posted on 2011-03-24
2
886 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
Comment Utility
0
 

Author Comment

by:Sheritlw
Comment Utility
Thank you aqarwalrahul!

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now