Click here to Skip to main content
15,891,874 members
Articles / Web Development / HTML

How to insert hidden input HTML tags into an ASP.NET page so that the ID or name attributes are not rewritten

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
24 Aug 2009CPOL1 min read 555.5K   13   6
How to insert hidden input HTML tags into an ASP.NET page so that the ID or name attributes are not rewritten.

Introduction

Recently, I was tasked with interfacing with a payment gateway that required I post information via hidden input fields. I ran into problems, specifically with ASP.NET rewriting the names of my input IDs causing the gateway to ignore them. I tried several different approaches to solve this, but since I am using masterpages, nothing seemed to work.

Solution 1

My solution was to create a custom control. I included properties for “name”, “id”, and “value”. Simply, all this control will do is write out “<input type="hidden" name="" id="" value="" />” with the values for each property.

Here is the code for the control:

C#
using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebCustomControls.HiddenField
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:hiddenField runat="server"></{0}:hiddenField>")]
    public class hiddenField : WebControl
    {    

        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Value
        {
            get
            {
                var s = (String)ViewState["Value"];
                return (s ?? String.Empty);
            }
            set
            {
                ViewState["Value"] = value;
            }

        }    

        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override string ID
        {
            get
            {
                var s = (String)ViewState["ID"];
                return (s ?? String.Empty);
            }
            set
            {
                ViewState["ID"] = value;
            }
        }

        [Bindable(false)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Name
        {
            get
            {
                var s = (String)ViewState["Name"];
                return (s ?? String.Empty);
            }
            set
            {
                ViewState["Name"] = value;
            }

        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            var sb = new StringBuilder();
            sb.AppendFormat(@"<input type=""hidden"" " + 
                            @"name=""{0}"" id=""{1}"" value=""{2}"" />", 
                            Name, ID, Value);

            output.Write(sb.ToString());
        }

    }
}

Since I am adding these tags dynamically via code, the HTML produced by ASP.NET will look something like below. ASP.NET added the span tags, but I can live with that.

HTML
<span id="ctl00_ctl07">
     <input type="hidden" name="CID" id="CID" value="123456" />
</span>
<span id="ctl00_ctl08">
      <input type="hidden" name="AMOUNT" id="AMOUNT" value="1" />
</span>

Here is some sample VB code to add an instance of the new control to your form:

VB
Dim hidElem As New hiddenField()
hidElem.EnableViewState = False
hidElem.ID = id
hidElem.Name = id
hidElem.Value = val

Me.Form.Controls.Add(hidElem)

Solution 2

In the process of working through this, I found a better and much simpler solution. Apparently, Microsoft has already included the solution in the CLR.

Just use:

VB
ClientScript.RegisterHiddenField("name","value")

One last thing

I also needed to post to a dynamic URL based off of a config file. I found I could set the form variables like this:

VB
Me.Form.Method = "post"
Me.Form.Action = URL

Or even better, just include the URL in the PostBackUrl for the Button control.

VB
buttonYes.PostBackUrl = URL

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer RocketDillo
United States United States
I have been developing software since 1991. Most of my early years were using COBOL on a mainframe. Fortunately, several years back I made the switch to .NET web development. The lime green text burned into my eyeballs may never go away.

Comments and Discussions

 
GeneralConfused Pin
Member 190497031-Aug-09 14:24
Member 190497031-Aug-09 14:24 
GeneralRe: Confused Pin
TexasMensch31-Aug-09 16:58
professionalTexasMensch31-Aug-09 16:58 
Right, but I needed to dynamically add the hidden input tags based on the user response.
GeneralToo late Pin
AWdrius24-Aug-09 21:53
AWdrius24-Aug-09 21:53 
GeneralGood Pin
Abhishek Sur24-Aug-09 21:36
professionalAbhishek Sur24-Aug-09 21:36 
GeneralAdditional span tags Pin
Michael Schmitt18-Aug-09 4:17
Michael Schmitt18-Aug-09 4:17 
GeneralRe: Additional span tags Pin
TexasMensch24-Aug-09 9:50
professionalTexasMensch24-Aug-09 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.