繁体   English   中英

如何将HTML元素的值传递给C#代码隐藏方法?

[英]How do I pass an HTML element's value to a C# code-behind method?

例如,现在我的ASPX如下所示:

<form name="form" runat="server" onsubmit="validate(this)">
    <table width="100%" height="100%">
        <tr>
            <td class="label">
                Start Date:
            </td>
            <td>
                <input type="text" name="StartDate" value='<%=GetCurrentDate("- testParam")%>' maxlength="10" /> 
            </td>
        </tr>
    </table>
</form>

..和我的C#如下:

public static string GetCurrentDate(string str)
{
    return DateTime.Now.ToString("MM/dd/yyyy") + str;
}

这可以正常工作,并输出“ 03/08/2017-testParam”。 但是,例如,如果不是要像我上面那样手动发送硬编码字符串,而是要从ASPX端传递HTML元素的值之一作为参数怎么办? 像这样:

...
<tr>
    <td class="label">
        Start Date:
    </td>
    <td>
        <input type="text" name="StartDate" value="<%=GetCurrentDate(formObj.elements.item('someLabel').value)%>" maxlength="10" />
    </td>
</tr>
...

我需要做什么才能将ASPX页上的“ someLabel”元素的值获取到C#页? 任何帮助,将不胜感激。

此方法GetCurrentDate运行在服务器端,但是此formObj.elements.item('someLabel').value在客户端运行

尝试这个..

<tr>
<td class="label">
    Start Date:
</td>
<td>
    <input type="text" name="StartDate" value='<%=GetCurrentDate()%>' maxlength="10" /> 
</td>

    public  string GetCurrentDate()
    {
        return DateTime.Now.ToString("MM/dd/yyyy");
    }

用于从服务器读取名为StartDate的输入的值。

string postValue =  Request.Form["StartDate"]

如果您想将值从客户端传递到后面的代码而不回传整个页面 ,则需要使用Ajax。

在ASP.Net Web窗体中调用服务器端方法并不像ASP.Net Web API或MVC那样干净。 您将需要使用旧的WebMethod

例如,

在此处输入图片说明

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <button type="button" onclick="getData();">Get Data</button>
        <br/>
        <input type="text" name="StartDate" id="txtStartDate" maxlength="10" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            function getData() {
                var data = {value: "test"};
                $.ajax({
                    type: "POST",
                    url: '<%= ResolveUrl("~/Default.aspx/GetCurrentDate") %>',
                    data: JSON.stringify(data),
                    contentType: "application/json",
                    success: function (msg) {
                        $("#txtStartDate").val(msg.d);
                    }
                });
            }
        </script>
    </form>
</body>
</html>

背后的代码

using System;
using System.Web.Script.Serialization;

namespace DemoWebForm
{
    public partial class Default : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        public static string GetCurrentDate(string value)
        {
            return new JavaScriptSerializer().Serialize(
                string.Format("{0} - {1}", DateTime.Now, value));
        }
    }
}

您可以通过添加runat =“ server”使其成为服务器控件,它将在文件后面的代码中可用。 或者,如果您不喜欢这样做,则在文件后面的代码中使用Request.Form [“ Name”]。 这里的“名称”是您为文本框控件指定的名称。

在您的情况下,名称为StartDate

因此,请尝试使用Request.Form [“ StartDate”]从后面的代码访问文本框的值

阅读本文。https://www.aspsnippets.com/Articles/Get-value-of-HTML-Input-TextBox-in-ASPNet-code-behind-using-C-and-VBNet.aspx

您应该将要发送到Web服务器的值以html形式发布。

暂无
暂无

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

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