如果已经在当前月份点击了按钮,则禁用该按钮

时间:2015-10-26 10:36:51

标签: javascript c# jquery asp.net

我有一个asp.net随机生成器Web应用程序,它有一个按钮,可以随机输入并在一个字段中显示结果并将其写入我的数据库并显示在网站上的表格中。

我被告知,这个按钮只允许每月点击一次,所以一旦被点击它就不应再被允许了。

我遇到的问题是我根本不知道如何做到这一点,因为我是开发人员的新手。

我知道我需要寻找的只是月份。我已经为我现在所拥有的内容提供了HTML和代码。

HTML

     <div class="row">
                <div class="col-sm-4">
                    <asp:TextBox class="form-control" ID="txtDestination" runat="server" disabled></asp:TextBox>
                </div>
                <div class="col-sm-2">
                    <% if (txtDestination.Text == "")
                    {%>
                        <asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
                    <%}
                    else
                    { %>
                        <button class="btn btn-success" disabled>Decided</button>
                    <%} %>
                </div>
            </div>    
<asp:GridView ID="tblPastPlaces" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlPastPlaces" GridLines="None" Width="30%">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Past_Place" HeaderText="Past Place" SortExpression="Past_Place" HeaderStyle-Width="60%" />
            <asp:BoundField DataField="Month" HeaderText="Month" SortExpression="Month" />
        </Columns>
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlPastPlaces" runat="server" ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" SelectCommand="GetPastPlaces" SelectCommandType="StoredProcedure" InsertCommand="INSERT INTO [Past_Places] ([Past_Place], [Month], [Click_Date]) VALUES (@Past_Place, @Month, @Click_Date)">
        <InsertParameters>
            <asp:Parameter Name="Past_Place" Type="String" />
            <asp:Parameter DbType="String" Name="Month" />
            <asp:Parameter DbType="Date" Name="Click_Date" />
        </InsertParameters>
    </asp:SqlDataSource>    

代码

SqlPastPlaces.InsertParameters["Past_Place"].DefaultValue = ((TextBox)txtDestination).Text;
SqlPastPlaces.InsertParameters["Month"].DefaultValue = DateTime.Now.ToString("MMMM");
SqlPastPlaces.InsertParameters["Click_Date"].DefaultValue = DateTime.Now.ToString();
SqlPastPlaces.Insert();

2 个答案:

答案 0 :(得分:0)

如果您尝试将数据库中最后点击日期的月份与当月比较,您可以执行以下操作:

string lastClickDateValue = GetMonthClickedFromDatabase(); //Connect to database and get the value within this method
DateTime lastClickDate = Convert.ToDateTime(lastClickDateValue);

if(lastClickDate.Month == DateTime.Now.Month)
{
     btnMonth.Enabled = false; //Button ID you want to disable
}

此代码旨在为您提供一些想法,您可以尝试使用所需的确切逻辑。

答案 1 :(得分:0)

通过从我的数据库中获取值然后添加隐藏的label并使用它来匹配datetime.now.tostring("MMMM")

来管理它

代码背后

protected void Page_Load(object sender, EventArgs e)
        {
            // Returns the last month entry from the Past_Places table so can disable the button if it has been run for the current month
            string connection = ConfigurationManager.ConnectionStrings["PaydayLunchConnectionString1"].ConnectionString;
            SqlConnection conn = new SqlConnection(connection);

            SqlCommand com = new SqlCommand(
                "SELECT        TOP (1) Month " +
                "FROM          Past_Places", conn);

            try
            {
                conn.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    PastMonth.Text = reader["Month"].ToString();
                }
                reader.Close();
            }
            finally
            {
                conn.Close();
            }
        }

查看

    <div class="row">
        <asp:Label ID="PastMonth" runat="server" Visible="false"></asp:Label>
        <div class="col-sm-4">
            <asp:TextBox class="form-control" ID="txtDestination" runat="server" disabled></asp:TextBox>
        </div>
        <div class="col-sm-2">
            <% if (PastMonth.Text.Equals(DateTime.Now.ToString("MMMM")))
            { %>
              <button class="btn btn-success" disabled>Already Done For This Month</button>
            <%}
            else if (txtDestination.Text == "")
            {%>
                <asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
            <%}
            else
            { %>
                <button class="btn btn-success" disabled>Decided</button>
            <%} %>
        </div>
    </div>