Dynamically created Hidden field

Discussion in 'ASP .Net' started by =?Utf-8?B?Y25pY2ts?=, Jan 29, 2006.

  1. I have the following problem: I’m creating a hidden control dynamically in
    the page_load event handler using the following code

    C#:
    ..
    HtmlInputHidden Hidden1 = new HtmlInputHidden();
    Hidden1.Name = "Hidden1";
    Hidden1.Value = "This is the Hidden Value";
    this.PlaceHolder.Controls.Add(Hidden1);
    ..

    HTML:
    ..
    <form id="MainForm" method="post" runat="server">
    <asp:placeHolder Runat="server" ID="PlaceHolder"></asp:placeHolder>
    </form>
    ..

    I would expect the name of the hidden control in the resulting HTML code
    sent to the browser to be “Hidden1â€. However, the HTML produced looks like
    this:

    <input name="_ctl0" type="hidden" value="This is the Hidden Value" />

    I there a different way to name the control in code?
     
    =?Utf-8?B?Y25pY2ts?=, Jan 29, 2006
    #1
    1. Advertising

  2. On Sun, 29 Jan 2006 13:41:27 -0800, cnickl
    <> wrote:

    >I have the following problem: I’m creating a hidden control dynamically in
    >the page_load event handler using the following code
    >
    >C#:
    >.
    >HtmlInputHidden Hidden1 = new HtmlInputHidden();
    >Hidden1.Name = "Hidden1";
    >Hidden1.Value = "This is the Hidden Value";
    >this.PlaceHolder.Controls.Add(Hidden1);
    >.
    >
    >HTML:
    >.
    ><form id="MainForm" method="post" runat="server">
    ><asp:placeHolder Runat="server" ID="PlaceHolder"></asp:placeHolder>
    ></form>
    >.
    >
    >I would expect the name of the hidden control in the resulting HTML code
    >sent to the browser to be “Hidden1”. However, the HTML produced looks like
    >this:
    >
    ><input name="_ctl0" type="hidden" value="This is the Hidden Value" />
    >
    >I there a different way to name the control in code?


    If you want to use your own identifier for a control in ASP.NET you
    must set the ID property of the control. The code below will do what
    I think you want...

    HtmlInputHidden Hidden1 = new HtmlInputHidden();
    Hidden1.Name = "Hidden1";
    Hidden1.ID = "Hidden1"; // set the id
    Hidden1.Value = "This is the Hidden Value";
    this.PlaceHolder1.Controls.Add(Hidden1);

    Now your source (when viewed from the browser) will look like this:

    <input name="Hidden1" type="hidden" id="Hidden1" value="This is the
    Hidden Value" />


    Otis Mukinfus
    http://www.otismukinfus.com
    http://www.tomchilders.com
     
    Otis Mukinfus, Jan 30, 2006
    #2
    1. Advertising

  3. Thanks very much, that took care of it.
     
    =?Utf-8?B?Y25pY2ts?=, Jan 30, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msimmons
    Replies:
    0
    Views:
    498
    msimmons
    Jul 16, 2009
  2. Rick
    Replies:
    3
    Views:
    8,201
    Alexey Smirnov
    Apr 13, 2010
  3. GavMc
    Replies:
    4
    Views:
    342
    Evertjan.
    Sep 22, 2005
  4. Roy Adams

    writing from hidden field to text field

    Roy Adams, Dec 20, 2004, in forum: Javascript
    Replies:
    1
    Views:
    201
  5. Roy Adams
    Replies:
    3
    Views:
    270
    Dr John Stockton
    Dec 21, 2004
Loading...

Share This Page