ASP.NET C# 表单提交问题

本文关键字:问题 表单提交 NET ASP | 更新日期: 2023-09-27 18:31:34

>我已经用jqueryui-date选择器构建了一个表单 - 基本上,如果结束日期小于或等于开始时间,它需要显示一条消息,说明它必须大于开始时间,然后才能允许用户提交表单。看不到我哪里出错了。

以下代码提交

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);
        if (startDate >= DateTime.Now)
        {
            if (endDate <= startDate)
            {
                usrComment.Visible = true;
                //usrComment.Text = "Return time needs to be greater than pickup time IF same day";
                usrComment.Text = "Date =" + startDate + "Date 2 =" + endDate;
            }
            else
            {
                if (Page.IsValid)
                {
                    string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
                    int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
                    string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
                    string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);
                    string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
                    string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
                    string EmailToUser = txtEmail.Text;
                    string EmailSubject = "Quote Form submitted";
                    ****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);
                    StringBuilder html = new StringBuilder();
                    html.AppendLine("<ul>");
                    html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
                    html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
                    html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
                    html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
                    html.AppendLine("</ul>");
                    m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());
                    //Response.Redirect("/contact-us/quote-form-submitted.aspx");
                }
                usrComment.Text = "SUBMIT IT NOW!!!!!";
            }
        }
    }

j查询日期选取器

$(function () {
    function getDiff() {
        var from = $(".start").val();
        var till = $(".fin").val();
        var c = from.split("/");
        beg = new Date(c[2], c[1] - 1, c[0]);
        var d = till.split("/");
        en = new Date(d[2], d[1] - 1, d[0]);
        var rest = (en - beg) / 86400000;
        var txt = rest == 0 ? "" : rest + " days"
        $("#res").text(txt);
    }
    $(".start").datepicker({
        changeMonth: false,
        changeYear: false,
        showAnim: "fadeIn",
        gotoCurrent: true,
        minDate: 0, //change this to +3 to start 3 days from now
        dateFormat: "dd/mm/yy",
        onSelect: function (dateText, inst) {
            $(".fin").val(dateText);
            $(".fin").datepicker("option", "minDate", dateText);
            getDiff();
        }
    });
    $(".fin").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        showAnim: "fadeIn",
        onSelect: getDiff
    });
    //Disabling Copy, Paste, Cut
    $('.start').bind('paste', function (e) {
        e.preventDefault();
        //alert("You cannot paste text into this textbox!");
        window.alert = function () { };
    });
    $('.fin').bind('paste', function (e) {
        e.preventDefault();
        //alert("You cannot paste text into this textbox!");
        window.alert = function () { };
    });
});

因此,如果您的取件日期为 17/09/2013,取件时间为 08:00,并且返回日期和时间相同,它应该通过消息提醒您,如果返回日期大于或等于开始时间,则返回取件时间需要大于 08:00 如果这有意义?

ASP.NET C# 表单提交问题

有一个有用的、建设性的评论会很好。这是否是"正确的方法"并不重要。我相信这就是你要做的。我刚刚在初始 if 语句中添加了一个 else,以通知用户选择晚于现在的开始日期,并更改了另一个 else 语句的文本以通知用户选择晚于开始日期的返回日期。

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);
        if (startDate >= DateTime.Now)
        {
            if (endDate <= startDate)
            {
                usrComment.Visible = true;
                usrComment.Text = "Please enter a Return date later than " + startDate;
            }
            else
            {
                if (Page.IsValid)
                {
                    string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
                    int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
                    string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
                    string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);
                    string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
                    string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
                    string EmailToUser = txtEmail.Text;
                    string EmailSubject = "Quote Form submitted";
                    ****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);
                    StringBuilder html = new StringBuilder();
                    html.AppendLine("<ul>");
                    html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
                    html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
                    html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
                    html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
                    html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
                    html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
                    html.AppendLine("</ul>");
                    m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());
                    //Response.Redirect("/contact-us/quote-form-submitted.aspx");
                }
                usrComment.Text = "SUBMIT IT NOW!!!!!";
            }
        }
        else
        {
            usrComment.Visible = true;
            usrComment.Text = "Please enter a Start date later than " + DateTime.Now;
        }
    }

我建议你不要使用任何类型的自定义函数。JquerUI日期选择器具有内置功能,用于比较结束日期和开始日期。

请尝试此

http://jqueryui.com/datepicker/#date-range