Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-24
2
Medium Priority
?
898 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
Rahul Agarwal earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 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