我测试了一些代码,这些代码通过减去2个日期并回溯两天之间的天数来计算天数。
这是第一次测试:
CalculateDaysTest1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalculateDaysTest1.aspx.cs" Inherits="PopeyeMarinaWebApp.Templates.CalculateDaysTest1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Calculate Dates Test 1</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="formitem">
<asp:Label ID="startDateLabel" CssClass="formlabel" runat="server" Text="Start Date:"></asp:Label>
<asp:TextBox ID="txtStartDate" CssClass="DatePicker" runat="server" AutoPostBack="True" OnTextChanged="calculateDays"></asp:TextBox>
</div>
<div class="formitem">
<asp:Label ID="endDateLabel" CssClass="formlabel" runat="server" Text="End Date:"></asp:Label>
<asp:TextBox ID ="txtEndDate" CssClass="DatePicker" runat="server" AutoPostBack="True" OnTextChanged="calculateDays"></asp:TextBox>
</div>
<div class="formitem">
<asp:Label ID="totalDaysLabel" CssClass="formlabel" runat="server" Text="Number of Days:"></asp:Label>
<asp:Label ID="totalDays" CssClass="idlabel" runat="server" Text=""></asp:Label>
</div>
</div>
<script>
$(function () {
$('.DatePicker').datepicker(
{
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: '1950:2100'
});
})
</script>
</form>
</body>
</html>
CalculateDaysTest1.cs
using System;
namespace PopeyeMarinaWebApp.Templates
{
public partial class CalculateDaysTest1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void calculateDays(object sender, EventArgs e)
{
string startdate = txtStartDate.Text;
string enddate = txtEndDate.Text;
if (startdate != "" && enddate != "")
{
DateTime t1 = Convert.ToDateTime(startdate);
DateTime t2 = Convert.ToDateTime(enddate);
totalDays.Text = t2.Subtract(t1).Days.ToString();
}
}
}
}
这里的一切都很完美,而且它正在做它应该做的事情。您可以选择开始日期和结束日期,并回溯计算的天数。
现在进行第二次测试我在使用模型绑定的表单视图控件的上下文中使用它。
这是测试2:
CalculateDaysTest2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalculateDaysTest2.aspx.cs" Inherits="PopeyeMarinaWebApp.Templates.CalculateDaysTest2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Calculate Dates Test 2</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server"
ItemType="PopeyeMarinaWebApp.Models.Lease"
DataKeyNames="LeaseID"
RenderOuterTable="false"
DefaultMode="Insert"
InsertMethod="LeaseFormAdd_InsertItem">
<InsertItemTemplate>
<fieldset>
<div class="formitem">
<asp:Label ID="startDateLabel" CssClass="formlabel" runat="server" Text="Start Date:"></asp:Label>
<asp:TextBox ID="txtStartDate" CssClass="DatePicker" runat="server" AutoPostBack="True" OnTextChanged="calculateDays" Text="<%#: BindItem.StartDate %>"></asp:TextBox>
</div>
<div class="formitem">
<asp:Label ID="endDateLabel" CssClass="formlabel" runat="server" Text="End Date:"></asp:Label>
<asp:TextBox ID ="txtEndDate" CssClass="DatePicker" runat="server" AutoPostBack="True" OnTextChanged="calculateDays" Text="<%#: BindItem.EndDate %>"></asp:TextBox>
</div>
<div class="formitem">
<asp:Label ID="totalDaysLabel" CssClass="formlabel" runat="server" Text="Number of Days:"></asp:Label>
<asp:Label ID="totalDays" CssClass="idlabel" runat="server" Text=""></asp:Label>
</div>
<div class="col-lg-12">
<asp:Button ID="InsertButton" runat="server" Text="Insert" CausesValidation="True" CssClass="btn btn-primary btn-sm" CommandName="Insert" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" CssClass="btn btn-primary btn-sm" CommandName="Cancel"/>
</div>
</fieldset>
</InsertItemTemplate>
</asp:FormView>
</div>
<script>
$(function () {
$('.DatePicker').datepicker(
{
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: '1950:2100'
});
})
</script>
</form>
</body>
</html>
CalculateDaysTest2.cs
using System;
using PopeyeMarinaWebApp.Models;
namespace PopeyeMarinaWebApp.Templates
{
public partial class CalculateDaysTest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void LeaseFormAdd_InsertItem()
{
var item = new Lease();
TryUpdateModel(item);
if (ModelState.IsValid)
{
using (MarinaDBContext db = new MarinaDBContext())
{
db.Leases.Add(item);
db.SaveChanges();
}
}
}
protected void calculateDays(object sender, EventArgs e)
{
string startdate = txtStartDate.Text;
string enddate = txtEndDate.Text;
if (startdate != "" && enddate != "")
{
DateTime t1 = Convert.ToDateTime(startdate);
DateTime t2 = Convert.ToDateTime(enddate);
totalDays.Text = t2.Subtract(t1).Days.ToString();
}
}
}
}
我在calculateDays事件中收到错误消息: &#39;名称&#39; textStartDate&#39;在当前背景下不存在&#39; &#39;名称&#39; textEndDate&#39;在当前背景下不存在&#39; &#39;名称&#39; totalDays&#39;在当前上下文中不存在&#39;
我只是想知道为什么它不能在Form View控件中工作,我该如何修复它。
非常感谢任何帮助。
大卫
答案 0 :(得分:0)
您已将代码放在“asp:FormView”中,这会阻止您直接在后面的代码中访问TextBox等。
您需要设置一些东西来处理来自FormView的命令。在后面的代码中,你需要这样的东西 - 虽然下面的例子是我的头脑。
void FormView1_ItemCommand(Object sender, FormViewCommandEventArgs e)
{
TextBox txtStartDate = (TextBox)e.Item.FindControl("txtStartDate");
if (e.CommandName == "Insert")
{
// Your logic here
}
}
请注意,您需要使用aspx页面上FormView的“OnItemCommand”属性来连接此代码。
有关更广泛的示例,请参阅MSDN文档。
至于在后面的代码中的任何地方访问TextBox,你需要在FormView上使用FindControl方法。像这样的东西之一:
TextBox txtStartDate = (TextBox)FormView1.Row.FindControl("txtStartDate");
或者
TextBox txtStartDate = (TextBox)FormView1.InsertItem.FindControl("txtStartDate");