【问题标题】:put reset button in default for code in user control page在用户控制页面中为代码默认设置重置按钮
【发布时间】:2018-10-06 16:18:46
【问题描述】:

此代码在用户 control.ascx 页面中

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Dates.ascx.cs" Inherits="WebApplication3.Dates" %>

<b>Arivval Date: </b><br />  <br />
    <asp:Calendar runat="server" ID="Arivval" ></asp:Calendar><br />
 <b>Depart Date: </b> <br /> <br />
 <asp:Calendar runat="server" ID="Depart" ></asp:Calendar>

网页表单页面中的代码(default.ascx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HotelReservation.aspx.cs" Inherits="WebApplication3.HotelReservation" %>
<%@ Register TagPrefix="dt" TagName="Date" Src="~/Dates.ascx" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>
<asp:Panel  runat="server" ID="ph">
    <form id="form1" runat="server">
        <asp:ImageButton ImageUrl="~/HotelImageButton.jpg"  ID="HOTEL" runat="server"  PostBackUrl="~/HotelReservation.aspx" />
        <asp:ImageButton ImageUrl="~/CarImageButton.jpg"  ID="CAR" runat="server"  PostBackUrl="~/CarReservation.aspx" />

            <h1 > Hotel Search </h1>

                           <dt:Date id="d" runat="server" />  <br />
            <b>Nights:</b> <asp:TextBox id="num" runat="server" TextMode="Number" />
            <br /> <br/>
           <b> Room Type:</b> <br /> <br />
            <asp:RadioButton id="Superior" runat="server" text="Superior" GroupName="RoomType" /> <br />
            <asp:RadioButton id="Twin" runat="server" text="Twin" GroupName="RoomType" /> <br />
            <asp:RadioButton id="Triple" runat="server" text="Triple" GroupName="RoomType" /> <br />
            <asp:RadioButton id="DeLuxe" runat="server" text="DeLuxe" GroupName="RoomType" /> <br />
            <asp:RadioButton id="Studio" runat="server" text="Studio" GroupName="RoomType" /> <br />
            <br /> <br />
            <asp:Button ID="search" Text="Search" runat="server" />
            <asp:Button ID="Reset" Text="Reset"   runat="server" style="margin-left: 51px" Width="61px" OnClick="Clear" />


    </form>
    </asp:Panel>
    </body>
    </html>

在 default.ascx.cs 页面中,我想在用户控件中放置日历的重置按钮如何操作

protected void Clear(object sender, EventArgs e)
{
    num.Text = "";
    Boolean f = false;
    Superior.Checked = f;
    Twin.Checked = f;
    Triple.Checked = f;
    DeLuxe.Checked = f;
    Studio.Checked = f;
}

我想将 default.ascx.cs 放入明确的事件代码中,以使控制页面中的 2 个日历不被选中

【问题讨论】:

    标签: c# asp.net webforms user-controls reset


    【解决方案1】:

    在您的用户控件中为选定日期创建公共属性并在主机页面中使用它们。像这样的。

    <%@ Control Language="C#" ClassName="Dates" %>
    <script runat="server">
        public DateTime ArivvalDate
        {
            get { return Arivval.SelectedDate; }
            set { Arivval.SelectedDate = value; }
        }
        public DateTime DepartureDate
        {
            get { return Depart.SelectedDate; }
            set { Depart.SelectedDate = value; }
        }
    </script>
    <b>Arivval Date: </b><br />  <br />
        <asp:Calendar runat="server" ID="Arivval" ></asp:Calendar><br />
     <b>Depart Date: </b> <br /> <br />
     <asp:Calendar runat="server" ID="Depart" ></asp:Calendar>
    

    页面:

    <!DOCTYPE html>
    <%@ Page Language="C#" %>
    <%@ Register Src="~/misc/Dates.ascx" TagPrefix="dt" TagName="Date" %>
    
    <script runat="server">
        protected void Clear(object sender, EventArgs e)
        {
            lstRoomType.SelectedIndex = -1;
            d.ArivvalDate = DateTime.MinValue;
            d.DepartureDate = DateTime.MinValue;
        }
        protected void search_Click(object sender, EventArgs e)
        {
            test.Text = string.Format("Arrival: {0:M/d/yy}, Departure: {1:M/d/yy}, Room Type: {2}", d.ArivvalDate, d.DepartureDate, lstRoomType.SelectedValue);
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Reservation Form</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <h1>Hotel Search </h1>
    
                <dt:Date ID="d" runat="server" />
                <br />
                <b>Nights:</b>
                <asp:TextBox ID="num" runat="server" TextMode="Number" />
                <br />
                <br />
                <b>Room Type:</b>
                <br />
                <br />
                <%--use list instead of a set of individual RB --%>
                <asp:RadioButtonList runat="server" ID="lstRoomType">
                    <asp:ListItem Text="Superior" Value="Superior" />
                    <asp:ListItem Text="Twin" Value="Twin" />
                    <asp:ListItem Text="Triple" Value="Triple" />
                    <asp:ListItem Text="Deluxe" Value="Deluxe" />
                    <asp:ListItem Text="Studio" Value="Studio" />
                </asp:RadioButtonList>
                <br />
                <br />
                <asp:Button ID="search" Text="Search" runat="server" OnClick="search_Click" />
                <asp:Button ID="Reset" Text="Reset" runat="server" Style="margin-left: 51px" Width="61px" OnClick="Clear" />
                <br />
                <asp:Literal ID="test" runat="server" />
            </div>
        </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多