我正在编写一个用于创建预留的asp.net程序,其中我有一个连接到SQL数据库的listview控件。用户输入要发送到数据库的大部分信息(姓名,电话号码等)。但是有一些信息(例如当前日期/时间)我希望在添加到之前自动填充数据库。我想在代码隐藏(C#)中设置它,但我似乎无法找到正确的方法来做到这一点。任何想法都将不胜感激!
这是我目前的代码:
<%@ 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>
答案 0 :(得分:2)
使用EntityDataSource.Inserting或EntityDataSource.Updating事件来访问要保存的对象。
您可以访问entity属性来更改对象中的值,如下所示:
// 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;
...
}
答案 1 :(得分:1)
为什么不在数据库中设置要自动填充的字段(如示例中的datetime)? (我认为SQL Server有一个GetDate函数,它返回你可以使用默认列值的当前日期时间)