Creating a hidden field in codebehind and accessing it via clientside javascript

前端 未结 2 1164
陌清茗
陌清茗 2021-01-24 07:24

I want to add a hidden field programmatically to an asp.net page, read and change it via javascript. So far my code fails at reading the added hidden field.

Here is a si

相关标签:
2条回答
  • 2021-01-24 07:54

    You could try something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlInputHidden hidden2 = new HtmlInputHidden();
        hidden2.ID = "Here you will put the id of the control";
        hidden2.Value = "Here you will put your value";
        this.Controls.Add(hidden2);
    }
    

    At the top of your source code file, you have to add this statement:

    using System.Web.UI.HtmlControls;
    
    0 讨论(0)
  • 2021-01-24 07:58

    The main problem was that the following line was missing in the aspx page:

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    

    The codebehind was never executed.

    0 讨论(0)
提交回复
热议问题