2017-09-23 3 views
1

asp.netテキストボックスコントロールの到着日を設定するためにcurrentDate変数を使用するにはどうすればよいですか?

<%@ 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>

私は(2017年10月10日のように - 2017年10月10日)到着日を設定するcurrentDate変数を使用する方法を把握しようとしています毎回そのコントロールのボックス内のページの負荷を。ここで私が自分で試したコードですが、ページをテストするたびに、そのコントロールのテキストボックスには何も自動的に入れられません。誰か助けてくれますか?それ以外のソースは他にもあります(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; 

     } 




    } 
} 
+0

@Federico Dipuma私を助けることができますか? – dorakta

+0

コードについては、Windows *の言語設定に応じて '9/23/2017' *を表示する必要があります。何がうまくいかず、期待される結果は何ですか? – Win

+0

@Win私は、ページが読み込まれるたびに、自分のウェブページのテキスト到着日コントロールに、事前に定義された日付10/10/2017を自動的に設定したいと思います。それは単にmm/dd/yyyyを出力します。手伝って頂けますか? – dorakta

答えて

1

私は、これはあなたが探しているものだと思う: - あなたの.aspxページでは

: - あなたの.aspx.csページで

<asp:Label ID="lblYear" runat="server"></asp:Label> 
    <asp:TextBox ID="txtArrivalDate" runat="server"></asp:TextBox> 

: -

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; 
    } 
} 
+0

@ Deepak-しかし、これを行うには、currentDateという名前の既存の変数を使用しますか? – dorakta

+0

編集済みの回答。 – Deepak

+0

@ Deepak-ご協力いただきありがとうございます! – dorakta

関連する問題