如何将HTML元素的值传递给C#代码隐藏方法? [英] How do I pass an HTML element's value to a C# code-behind method?
问题描述
< form name =formrunat =服务器onsubmit =validate(this)>
< table width =100%height =100%>
< tr>
< td class =label>
开始日期:
< / td>
< td>
< input type =textname =StartDatevalue ='<%= GetCurrentDate( - testParam)%>'maxlength =10/>
< / td>
< / tr>
< / table>
< / form>
..和我的C#如下:
< pre $ public static string GetCurrentDate(string str)
{
return DateTime.Now.ToString(MM / dd / yyyy)+ str;
}
这很好,输出03/08/2017 - testParam。但是,举个例子,如果不像上面那样手动发送硬编码字符串,我想从ASPX端传入一个HTML元素的值作为参数?像这样:
...
< tr>
< td class =label>
开始日期:
< / td>
< td>
< input type =textname =StartDatevalue =<%= GetCurrentDate(formObj.elements.item('someLabel').value)%> maxlength =10/>
< / td>
< / tr>
...
获取someLabel元素的值需要做些什么在我的ASPX页面上的C#页面?任何与此有关的帮助将不胜感激。
如果您想将值从客户端传递到代码不回发整个页面,您将需要使用Ajax。
调用ASP.Net Web Form中的服务器端方法并不像ASP.Net Web API或MVC那样干净。您需要使用旧的 WebMethod 。
>
< %@ Page Language =C#AutoEventWireup =trueCodeBehind =Default.aspx.csInherits =DemoWebForm.Default%>
<!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>< / title>
< / head>
< body>
< form id =form1runat =server>
< button type =buttononclick =getData();>获取数据< / button>
< br />
< input type =textname =StartDateid =txtStartDatemaxlength =10/>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>< / script>
< script type =text / javascript>
函数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>
代码隐藏
使用系统;
使用System.Web.Script.Serialization;
命名空间DemoWebForm
{
public partial class默认值:System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static String GetCurrentDate(string value)
{
return new JavaScriptSerializer()。Serialize(
string.Format({0} - {1},DateTime.Now,值));
}
}
}
For example, right now I have my ASPX like so:
<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>
..and my C# as follows:
public static string GetCurrentDate(string str)
{
return DateTime.Now.ToString("MM/dd/yyyy") + str;
}
This works fine and outputs "03/08/2017 - testParam". But what if, for example, instead of sending a hardcoded string manually as I did above, I want to pass in one of the HTML element's values as a parameter from the ASPX side? Like this:
...
<tr>
<td class="label">
Start Date:
</td>
<td>
<input type="text" name="StartDate" value="<%=GetCurrentDate(formObj.elements.item('someLabel').value)%>" maxlength="10" />
</td>
</tr>
...
What do I need to do to get the "someLabel" element's value on my ASPX page to the C# page? Any help with this will be greatly appreciated.
If you want to pass a value from client-side to code behind without posting back the entire page, you will need to use Ajax.
Calling to a server-side method in ASP.Net Web Form is not as clean as ASP.Net Web API or MVC. You will need to use old WebMethod.
For example,
<%@ 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>
Code Behind
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));
}
}
}
这篇关于如何将HTML元素的值传递给C#代码隐藏方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!