简体   繁体   English

提交表单期间如何修改表单字段值?

[英]How can I modify a form field value during form submission?

I'm developing a form using ASP.NET form controls. 我正在使用ASP.NET表单控件开发表单。 One of the form fields is Date of Birth. 表单字段之一是“出生日期”。 The date format is in dd/mm/yyyy. 日期格式为dd / mm / yyyy。 What I need to do is actually convert this form field to mm/dd/yyyy once the user submits the form but before the data goes to the server because the system the form data is going to is forcing US date formats (mm/dd/yyyy) which I can't control, hence all date formats in dd/mm/yyyy will be reversed so they will be incorrect. 我需要做的是实际上是在用户提交表单之后但在数据发送到服务器之前将此表单字段转换为mm / dd / yyyy,因为系统要使用的表单数据正在强制使用美国日期格式(mm / dd / yyyy)(我无法控制),因此dd / mm / yyyy中的所有日期格式都将被颠倒,因此它们将是不正确的。 So I need to actually switch it before hand, so its actually reversed back to the correct format. 因此,我实际上需要事先进行切换,以便将其实际上恢复为正确的格式。 Here's what I've wrote in order to switch the value. 这是我为切换值而写的内容。

string userDOB = Request.Form["txtDOB2"];
DateTime convertedDOB = Convert.ToDateTime(userDOB);
txtDOB2.Text = convertedDOB.ToString("MM/dd/yyyy");

Note: I am checking to make sure the Date of Birth field not null or empty etc, just posting the specific code related to my question. 注意:我正在检查以确保“出生日期”字段不为null或为空等,仅发布与我的问题相关的特定代码。

I'm running this in code behind via a submit button click action. 我通过提交按钮单击操作在代码后面运行它。 So far, a test label just appends the switched date format value to confirm its working. 到目前为止,测试标签仅附加了转换日期格式值以确认其工作正常。 However I'm unsure how to switch the form value itself so it ends up in the actual posted form data. 但是我不确定如何切换表单值本身,以便最终显示在实际发布的表单数据中。 Request.Form is read only, so I don't know how to assign the modified date to the form field value. Request.Form是只读的,所以我不知道如何将修改日期分配给表单字段值。

Another potential problem is the form action is to an external source, outside of the domain the form is running on. 另一个潜在的问题是,表单操作是针对外部源的,该外部源在运行表单的域之外。

This would be how I'd attack it: 这就是我的攻击方式:

FormatDatesModule.cs FormatDatesModule.cs

public class FormatDatesModule : IHttpModule
{
    private static readonly Regex dateFilter = new Regex(@"^(?<d>\d{2})\/(?<m>\d{2})\/(?<y>\d{4})$", RegexOptions.Compiled);

    public void Init(HttpApplication context)
    {
        context.BeginRequest += (sender,e) => {
            HttpRequest request = ((HttpApplication)sender).Request;

            if (request.QueryString.Count > 0)
            {
                this.FormatDatesInCollection(request.QueryString);
            }

            if (request.HttpMethod == "POST" && request.Form.Count > 0)
            {
                this.FormatDatesInCollection(request.Form);
            }
        };
    }

    private static void FormatDatesInCollection(NameValueCollection parameters)
    {
        // Bypass readonly
        PropertyInfo isReadOnly = parameters.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
        isReadOnly.SetValue(parameters, false, null);

        for (var i = 0; i < parameters.Count; i++)
        {
            Match dateMatch = dateFilter.Match(parameters[i]);
            if (dateMatch.Success)
            {
                parameters[i] = String.Join("/", dateMatch.Groups["m"].Value, dateMatch.Groups["d"].Value, dateMatch.Groups["y"].Value);
            }
        }

        isReadOnly.SetValue(parameters, true, null);
    }

    public void Dispose()
    {
    }
}

web.config web.config中

<configuration>
    <system.web>
        <httpModules>
            <add name="FormatDatesModule" type="FormatDatesModule"/>
        </httpModules>
    </system.web>
    <system.webServer> <!-- IIS 7 Integrated Mode -->
        <modules>
            <add name="FormatDatesModule" type="FormatDatesModule"/>
        </modules>
    </system.webServer>
</configuration>

You can write this code on button click event. 您可以在按钮单击事件上编写此代码。 for eg 例如

    textBox1.Text="31/03/2014"

then 然后

   Button_click()
   {
        DateTime MyDateTime = new DateTime();
        MyDateTime = DateTime.ParseExact(textBox1.Text, "MM/dd/yyyy", null);
        textBox2.Text = MyDateTime.ToString("dd/MM/yyyy");
   }

OR Directly you can use 或直接使用

   textBox2.Text=(DateTime.ParseExact(textBox1.Text, "MM/dd/yyyy",null).ToString("dd/MM/yyyy"));

First you have to Convert it to DateTime format, then again in string. 首先,您必须将其转换为DateTime格式,然后再次转换为字符串。 Hope that would be helpful for you. 希望对您有所帮助。

After reviewing everything I've decided to use a WebRequest: 复习完所有内容后,我决定使用WebRequest:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

With the form action being an external URL (not on the same domain) I have come to the conclusion I am unable to modify the form data. 由于表单操作是一个外部URL(不在同一域中),因此得出的结论是我无法修改表单数据。 However, by allowing the form to postback and specifying the form action and everything else in code behind I'm able to specify the DOB value to send, using Himanshu's DOB conversion method, I was able to POST a form with the DOB switched. 但是,通过允许表单回发并指定表单动作以及后面代码中的所有其他内容,我能够使用Himanshu的DOB转换方法指定要发送的DOB值,从而能够在切换DOB的情况下发布表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM