簡體   English   中英

如何使用 currentDate 變量在 asp.net 文本框控件中設置到達日期?

[英]How can I use my currentDate variable to set the arrival date in the asp.net textbox control?

 <%@ 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 --> &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <asp:RadioButton ID="RadioButton1" runat="server" Text="King" Selected="True" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:RadioButton ID="RadioButton2" runat="server" Text="Two Queens"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <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>&copy; <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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM