如何使用我的currentDate变量在asp.net文本框控件中设置到达日期?
问题描述:
如何使用我的currentDate变量在asp.net文本框控件中设置到达日期?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Request.aspx.cs" Inherits="XEx06Reservation.Request" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Chapter 6: Reservations</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/site.css" rel="stylesheet" />
<style type="text/css">
.auto-style1 {
display: block;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
.auto-style2 {
left: 0px;
top: 0px;
width: 1112px;
height: 64px;
}
</style>
</head>
<body>
<div class="container">
<header class="jumbotron">
<img src="Images/logo.png" alt="Royal Inns and Suites" />
</header>
<main>
<form id="form1" runat="server" class="form-horizontal">
<h1>Reservation Request</h1>
<h3>Request data</h3>
<div class="form-group">
<label class="col-sm-3 control-label">Arrival Date</label>
<div class="col-sm-4">
<asp:TextBox ID="txtArrivalDate" runat="server" CssClass="form-control" TextMode="Date"
Height="39px" Width="186px"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Departure Date</label>
<div class="col-sm-4">
<!-- text box -->
<asp:TextBox ID="TextBox1" runat="server" CssClass="form-control" TextMode="Date" Height="39px" Width="186px"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Number of people</label>
<div class="col-sm-4">
<!-- drop-down -->
<asp:DropDownList ID="DropDownList3" runat="server" Height="39px" Width="186px">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Bed type</label>
<div class="auto-style2">
<!-- radio buttons -->
<asp:RadioButton ID="RadioButton1" runat="server" Text="King" Selected="True" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="Two Queens"/>
<asp:RadioButton ID="RadioButton3" runat="server" Text="One Queen"/>
</div>
</div>
<h3>Special requests</h3>
<div class="form-group">
<div class="col-sm-7">
<!-- multiline text box -->
<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control" TextMode="MultiLine" Rows="4" Width="334px" ></asp:TextBox>
</div>
</div>
<h3>Contact information</h3>
<div class="form-group">
<label class="col-sm-3 control-label">First Name</label>
<div class="col-sm-4">
<asp:TextBox ID="txtFirstName" runat="server" CssClass="auto-style1" Width="153px" Height="36px"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Last Name</label>
<div class="col-sm-4">
<!-- text box -->
<asp:TextBox ID="TextBox3" runat="server" CssClass="form-control" Width="158px"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email address</label>
<div class="col-sm-4">
<!-- text box -->
<asp:TextBox ID="TextBox4" runat="server" CssClass="form-control" Width="159px" TextMode="Email"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Telephone number</label>
<div class="col-sm-4">
<!-- text box -->
<asp:TextBox ID="TextBox5" runat="server" CssClass="form-control" Width="155px" TextMode="Phone"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Preferred method</label>
<div class="col-sm-4">
<!-- drop down -->
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style1" Width="155px">
<asp:ListItem>Email</asp:ListItem>
<asp:ListItem>Telephone</asp:ListItem>
</asp:DropDownList>
</div>
</div>
<%-- Submit and Clear buttons --%>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
CssClass="btn btn-primary" />
<asp:Button ID="btnClear" runat="server" Text="Clear"
CssClass="btn btn-primary" />
</div>
</div>
<%-- message label --%>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-11">
<asp:Label ID="lblMessage" runat="server" CssClass="text-info"></asp:Label>
</div>
</div>
</form>
</main>
<footer>
<p>© <asp:Label ID="lblYear" runat="server"></asp:Label>
Royal Inns and Suites</p>
</footer>
</div>
</body>
</html>
我试图找出我如何可以使用currentDate
变量来设置的抵达日期(如2017年10月10日 - 2017年10月10日)在控制的盒子每次页面加载。这是我自己试过的代码,但每次我测试页面时,都没有任何东西会自动放入该控件的文本框中。有人能帮我吗?除此以外的任何其他来源(https://www.quora.com/How-do-I-populate-date-automatically-in-a-text-box-in-asp-net)?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace XEx06Reservation
{
public partial class Request : System.Web.UI.Page
{
private string currentDate = DateTime.Today.ToString("d");
private string currentYear = DateTime.Today.Year.ToString();
protected void Page_Load(object sender, EventArgs e)
{
txtArrivalDate.Text = currentDate;
}
}
}
答
我认为这是你在找什么: -
在.aspx页面: -
<asp:Label ID="lblYear" runat="server"></asp:Label>
<asp:TextBox ID="txtArrivalDate" runat="server"></asp:TextBox>
在你.aspx.cs页: -
public partial class WebForm1 : System.Web.UI.Page
{
private static readonly DateTime currentDate = new DateTime(2017, 10, 10);
private string currentYear = DateTime.Today.Year.ToString();
protected void Page_Load(object sender, EventArgs e)
{
txtArrivalDate.Text = currentDate.ToString();
lblYear.Text = currentYear;
}
}
@Federico Dipuma你能帮我吗? – dorakta
关于代码应该显示'9/23/2017' * *取决于Windows *的语言首选项。什么不起作用,你的预期结果是什么? – Win
@Win我想在每次页面加载时在我的网页上的文本到达日期控件中自动设置预定义日期10/10/2017。但它只是输出mm/dd/yyyy。你可以帮我吗? – dorakta