C# 使文本框隐藏在 ASP.NET 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1807454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:01:59  来源:igfitidea点击:

Making text box hidden in ASP.NET

c#htmlasp.net

提问by Etienne

I am using ASP.NET 3.5 and C#.

我正在使用 ASP.NET 3.5 和 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.

在我的页面上,我需要有一个用户不能看到的文本框,但是当您查看页面源时它必须在那里,原因是另一个名为 Eloqua 的程序将查看页面源并且它必须获得该文本框的值。

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.

因此,我无法将文本框属性设置为 Visible = False,因为它不会出现在源 HTML 中,并且我无法设置 Enabled = False,因为我不希望用户看到文本框。

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.NET 文本框

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

采纳答案by Quintin Robinson

You can use a hidden field.

您可以使用隐藏字段。

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

Use it just like a textbox.

像文本框一样使用它。

回答by matpol

Why not use a hidden field:

为什么不使用隐藏字段:

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

回答by Canavar

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

试试这个不可见的文本框而不是服务器端的 Visible 属性:

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

回答by fyjham

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).

第一个想法:你可以使用隐藏字段吗?这会更合适(<asp:hiddenfield ID="blah" runat="Server" /> 如果你想要一个 .NET 控件)。

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" />)

如果应用程序不接受,您实际上可以将“style='display: none;'”放在页面的代码前面。Intellisense 不会喜欢它,但它会呈现得很好(例如:<asp:TextBox id="txtField" style="display: none;" runat="server" />)

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

同样从代码隐藏你可以做 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;}"

或者你也可以给它一个 CssClass "hidden",它在你的 CSS 中被定义为 ".hidden { display: none;}"

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

CSS 类或仅使用隐藏字段将是我的建议。

回答by edosoft

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

如果由于某种原因它必须是一个文本框,只需用 css 隐藏它:

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

回答by Ondrej Slinták

CSS:

CSS:

.hidden-div
{
    display: none;
}

HTML:

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.

编辑:对不起,我误读了。我以为你想隐藏输入。但无论如何都无所谓,只需用基本上任何东西替换输入即可。

回答by Mick Walker

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

如何使用 CSS 隐藏包含文本框的 div:

.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.

希望这可以帮助。

回答by Anuraj

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

通过在服务器端设置 Visible = "false" 将不会呈现控件。您应该使用asp:HiddenINPUT type="hidden"。其他选项是使用 CSS,通过设置display:none.

回答by Mohamed El-Qassas MVP

Simply,Try to create a CSS classand attach it to your TextBoxas the following:

简单地,尝试创建一个CSS class并将其附加到您的TextBox,如下所示:

CSS Class Style

CSS 类样式

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

Textbox using CssClass="Hide"

文本框使用 CssClass="Hide"

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

Note:you can't use validation with the Hiddencontrol

注意:您不能对Hidden控件使用验证