簡體   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