ASP.NET C#表单提交问题

时间:2013-09-17 16:09:54

标签: c# javascript jquery asp.net forms

我用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!!!!!";
            }
        }
    }

日期选择器的jQuery

  

$(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,并且返回日期和时间相同,则应提醒您该消息以及退货日期是否大于或等于start如果有意义的话,返程时间需要大于08:00吗?

2 个答案:

答案 0 :(得分:1)

有一个有用的,有建设性的评论会很高兴。如果这是做到这一点的“正确方法”并不重要。我相信这就是你想要做的。我刚刚在初始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;
        }
    }

答案 1 :(得分:0)

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

请试试这个

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