Solved

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

Posted on 2011-03-24
2
891 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

23 Experts available now in Live!

Get 1:1 Help Now