Solved

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

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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