Etienne
Etienne

Reputation: 7201

Making text box hidden in ASP.NET

I am using ASP.NET 3.5 and C#.

On my page I need to have a Text box that must not be visible to the user but it MUST be there when you look at the Page Source, reason being, another program called Eloqua will be looking at the page source and it must get the value of that text box.

The value of that text box will be populated based on what the user selects.

Thus, I cant set the text box property to Visible = False because then it will not be in the source HTML and I can't set the Enabled = False because I do not want the user to see the text box.

Is there some property I can use to make this text box hidden to the user but still visible in the page source?

My ASP.NET text box

<asp:TextBox ID="txtTester" runat="server"></asp:TextBox>

Upvotes: 17

Views: 97417

Answers (11)

Jeyachandran s
Jeyachandran s

Reputation: 1

<asp:TextBox ID="TextBox2" runat="server" visible="false"></asp:TextBox>

Upvotes: 0

Mohamed
Mohamed

Reputation: 822

Simply, Try to create a CSS class and attach it to your TextBox as the following:

CSS Class Style

<style>
    .Hide {
        display:none;
    }
</style>

Textbox using CssClass="Hide"

<asp:TextBox ID="txtTester" runat="server" CssClass="Hide"></asp:TextBox>

Note: you can't use validation with the Hidden control

Upvotes: 0

edosoft
edosoft

Reputation: 17271

If it must be a textbox for whatever reason just hide it with css:

<input type="text" name="blah" style="display:none" />

Upvotes: 2

Anuraj
Anuraj

Reputation: 19598

By Setting Visible = "false" in server side will not render the control. You should either use asp:Hidden or INPUT type="hidden". Other option is using CSS, by setting display:none.

Upvotes: 1

Ondrej Slint&#225;k
Ondrej Slint&#225;k

Reputation: 31910

CSS:

.hidden-div
{
    display: none;
}

HTML:

<div class="hidden-div">
    <input ... />
</div>

It will cause your input to be hidden, but it's gonna be visible in source code.

EDIT: Sorry, I misread it. I thought you wanted to hide an input. But it doesn't matter anyway, just replace input with basically anything.

Upvotes: 2

Mick Walker
Mick Walker

Reputation: 3847

How about using CSS to hide a div containing the text box:

.hidden {
    position: absolute;
    left: -9999px;
} 

Then within your page:

<div class="hidden">
    <asp:TextBox ID="TextBox1" runat="server" Text="hi"></asp:TextBox>
</div>

Hope this helps.

Upvotes: 0

fyjham
fyjham

Reputation: 7034

First thought: Can you use a hidden field? This would be much more suitable (<asp:hiddenfield ID="blah" runat="Server" /> if you want a .NET control).

If the app won't take that though you can actually just put "style='display: none;'" in the code-infront of the page. Intellisense won't like it, but it'll render just fine (EG: <asp:TextBox id="txtField" style="display: none;" runat="server" />)

Also from the codebehind you can do txtField.Attributes.Add("style", "display: none");

Or you could also just give it a CssClass "hidden" which in your CSS is defined as ".hidden { display: none;}"

The CSS class or just using a hidden field would be my recommendations.

Upvotes: 9

Canavar
Canavar

Reputation: 48088

Try this to invisible textbox instead of server side Visible property :

myTextBox.Style.Add("visibility", "hidden");
// or :
myTextBox.Style.Add("display", "none");

Upvotes: 15

matpol
matpol

Reputation: 3074

Why not use a hidden field:

<input type="hidden" name="blah" />

Upvotes: 0

Quintin Robinson
Quintin Robinson

Reputation: 82335

You can use a hidden field.

<asp:HiddenField id="myHiddenInput" runat="server" />

Use it just like a textbox.

Upvotes: 26

Related Questions