我想根据用户输入的生日来将值传递给文本框。如果用户少于15年,则"Borrow Books"
字段的值应为4
,否则应为8
。请告诉我如何继续。
答案 0 :(得分:0)
在日历html中Selection_Change
OnSelectionChanged="Selection_Change"
日历事件,如下所示
void Selection_Change(Object sender, EventArgs e)
{
DateTime date1 = Calendar1.SelectedDate;
var dateDiff=DateTime.Today-date1 ;
var year=dateDiff.Year;
var totalBook=year > 15 ? 8: 4;
txtBorrowerBooks.Text =totalBook.ToString();
}
void Selection_Change(Object sender, EventArgs e)
{
DateTime date1 = Calendar1.SelectedDate;
DateTime zeroTime = new DateTime(1, 1, 1);
var dateDiff=DateTime.Today-date1 ;
var year=(zeroTime +dateDiff).Year;
var totalBook=year > 15 ? 8: 4;
txtBorrowerBooks.Text =totalBook.ToString();
}
答案 1 :(得分:0)
我认为您必须向asp.net日历添加一个事件:Calendar.SelectionChanged事件
然后您的代码将是这样的:
前端:
<asp:Calendar ID="calcBorrowerBorn" runat="server" OnSelectionChanged="Selection_Change" VisibleDate="2014-11-08"
BackColor="White" BorderColor="Black" BorderStyle="Solid" CellSpacing="1"
Font-Names="Verdana" Font-Size="9pt" ForeColor="Black" Height="250px"
NextPrevFormat="ShortMonth" ShowGridLines="True" Width="330px">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
Height="8pt" />
<DayStyle BackColor="#CCCCCC" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="#333399" ForeColor="White" />
<TitleStyle BackColor="#333399" BorderStyle="Solid" Font-Bold="True"
Font-Size="12pt" ForeColor="White" Height="12pt" />
<TodayDayStyle BackColor="#999999" ForeColor="White" />
</asp:Calendar>
后端:
void Selection_Change(Object sender, EventArgs e)
{
DateTime userSelectedDate = Calendar1.SelectedDate;
txtBorrowerBooks.Text = (GetAge(userSelectedDate) > 15)?"8":"4";
}
public Int32 GetAge(DateTime dateOfBirth)
{
var today = DateTime.Today;
var a = (today.Year * 100 + today.Month) * 100 + today.Day;
var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;
return (a - b) / 10000;
}