Search code examples
asp.netsqllistviewcode-behinddata-binding

ASP.NET Data Binding from CodeBehind


I am writing an asp.net program for creating reservations in which I have a listview control connected to an SQL database. The user enters most of the information to be sent to the database (name, phone-number, etc..) however there is some info (the current date/time for example) that I would like to be automatically populated before being added to the db. I would like to set this up in the code-behind (C#), but I cannot seem to figure out the proper way to do it. Any thoughts would be greatly appreciated!

Here is my current code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SummaryForm.ascx.cs" Inherits="RamRideOps.Controls.SummaryForm" %>

<asp:EntityDataSource ID="Rides_EDS" runat="server" 
ConnectionString="name=RamRideOpsEntities" 
DefaultContainerName="RamRideOpsEntities" EnableFlattening="False" 
EntitySetName="Rides" EntityTypeFilter="Ride" EnableDelete="True" 
EnableInsert="True" EnableUpdate="True">
</asp:EntityDataSource>

<asp:ListView ID="SummaryLV" runat="server" DataKeyNames="TimeOfCall" 
DataSourceID="Rides_EDS" InsertItemPosition="LastItem" 
onselectedindexchanged="ListView1_SelectedIndexChanged">
<AlternatingItemTemplate>
    <tr style="background-color:#FFF8DC;">           
        <td>
            <asp:Label ID="TimeOfCallLabel" runat="server" 
                Text='<%# Eval("TimeOfCall") %>' />
        </td>           
        <td>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
        </td>
        <td>
            <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
        </td>
        <td>
            <asp:Label ID="NumPatronsLabel" runat="server" 
                Text='<%# Eval("NumPatrons") %>' />
        </td>
        <td>
            <asp:Label ID="PickupAddressLabel" runat="server" 
                Text='<%# Eval("PickupAddress") %>' />
        </td>            
        <td>
            <asp:Label ID="DropoffAddressLabel" runat="server" 
                Text='<%# Eval("DropoffAddress") %>' />
        </td>
        <td>
            <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
        </td>
        <td>
            <asp:Label ID="AssignedCarLabel" runat="server" 
                Text='<%# Eval("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delt" />                
        </td>
    </tr>
</AlternatingItemTemplate>
<EditItemTemplate>
    <tr style="background-color:#008A8C;color: #FFFFFF;">
        <td>
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        </td>
        <td>
            <asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' />
        </td>
        <td>
            <asp:TextBox ID="NumPatronsTextBox" runat="server" 
                Text='<%# Bind("NumPatrons") %>' />
        </td>
        <td>
            <asp:TextBox ID="PickupAddressTextBox" runat="server" 
                Text='<%# Bind("PickupAddress") %>' />
        </td>            
        <td>
            <asp:TextBox ID="DropoffAddressTextBox" runat="server" 
                Text='<%# Bind("DropoffAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
        </td>
        <td>
            <asp:TextBox ID="AssignedCarTextBox" runat="server" 
                Text='<%# Bind("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Cancel" />
        </td>
    </tr>
</EditItemTemplate>
<EmptyDataTemplate>
    <table runat="server" 
        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
        <tr runat="server">
            <td runat="server">
                There are currently no scheduled rides!</td>
        </tr>
    </table>
</EmptyDataTemplate>
<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:TextBox ID="TimeOfCallTextBox" runat="server" 
                Text='<%# Bind("TimeOfCall") %>' Visible="False" />
        </td>
        <td>
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
        </td>
        <td>
            <asp:TextBox ID="PhoneTextBox" runat="server" Text='<%# Bind("Phone") %>' />
        </td>
        <td>
            <asp:TextBox ID="NumPatronsTextBox" runat="server" 
                Text='<%# Bind("NumPatrons") %>' />
        </td>
        <td>
            <asp:TextBox ID="PickupAddressTextBox" runat="server" 
                Text='<%# Bind("PickupAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="DropoffAddressTextBox" runat="server" 
                Text='<%# Bind("DropoffAddress") %>' />
        </td>
        <td>
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' Visible="True" />
        </td>
        <td>
            <asp:TextBox ID="AssignedCarTextBox" runat="server" 
                Text='<%# Bind("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Add" OnClientClick="addButton_Click" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Clear" />
        </td>
    </tr>
</InsertItemTemplate>
<ItemTemplate>
    <tr style="background-color:#DCDCDC;color: #000000;">     
        <td>
            <asp:Label ID="TimeOfCallLabel" runat="server" 
                Text='<%# Eval("TimeOfCall") %>' />
        </td>           
        <td>
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
        </td>
        <td>
            <asp:Label ID="PhoneLabel" runat="server" Text='<%# Eval("Phone") %>' />
        </td>
        <td>
            <asp:Label ID="NumPatronsLabel" runat="server" 
                Text='<%# Eval("NumPatrons") %>' />
        </td>
        <td>
            <asp:Label ID="PickupAddressLabel" runat="server" 
                Text='<%# Eval("PickupAddress") %>' />
        </td>            
        <td>
            <asp:Label ID="DropoffAddressLabel" runat="server" 
                Text='<%# Eval("DropoffAddress") %>' />
        </td>
        <td>
            <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
        </td>
        <td>
            <asp:Label ID="AssignedCarLabel" runat="server" 
                Text='<%# Eval("AssignedCar") %>' />
        </td>
        <td>
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delt" />                
        </td>
    </tr>
</ItemTemplate>

<LayoutTemplate>
    <table runat="server">
        <tr runat="server">
            <td runat="server">
                <table ID="itemPlaceholderContainer" runat="server" border="1" 
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                    <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                        <th runat="server" style="width:auto">
                            Call Time</th>                            
                        <th runat="server" style="width:auto">
                            Name</th>
                        <th runat="server" style="width:5px">
                            Phone</th>
                        <th runat="server" style="width:auto">
                            Size</th>
                        <th runat="server" style="width:auto">
                            Pick-Up</th>
                        <th runat="server" style="width:auto">
                            Drop-Off</th>
                        <th runat="server" style="width:auto">
                            Status</th>
                        <th runat="server" style="width:auto">
                            Car</th>
                        <th id="Th1" runat="server">
                            </th>
                    </tr>
                    <tr ID="itemPlaceholder" runat="server">
                    </tr>
                </table>
            </td>
        </tr>
        <tr runat="server">
            <td runat="server" 
                style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                <asp:DataPager ID="DataPager1" runat="server">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                            ShowLastPageButton="True" />
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
</LayoutTemplate>


Solution

  • Use the EntityDataSource.Inserting or EntityDataSource.Updating event to access the object you are saving.

    You can access the entity property to change the values in your object like this:

    // E.g. Inserting
    protected void Rides_EDS_DataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
        var rides = (e.Entity as Rides);
        rides.Date = DateTime.Now;
        rides.Total = Fee + Tax;
        ...
    }