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

后端 未结 4 1148
暗喜
暗喜 2021-01-26 14:27

For example, right now I have my ASPX like so:

4条回答
  •  粉色の甜心
    2021-01-26 14:37

    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" %>
    
    
    
    
    
        
    
    
        
            
            

    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));
            }
        }
    }
    

提交回复
热议问题