C# 如何使用 FindControl 获取 <input of type=hidden>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12930298/
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-10 00:58:34  来源:igfitidea点击:

how to get <input of type=hidden> with FindControl

c#asp.net

提问by user1292656

I am trying to get the value of a hidden input in code behind with the following code. I am trying to cast it but it cannot find it , any help ?

我正在尝试使用以下代码在后面的代码中获取隐藏输入的值。我正在尝试投射它但找不到它,有什么帮助吗?

((HtmlControl)FindControl("contentId"))

I declare it in aspx with the following code:

我在 aspx 中使用以下代码声明它:

    <input id="contentId"  type="hidden" />

I dont want to runat server because i have my own reasons

我不想运行服务器,因为我有自己的原因

采纳答案by Ramesh

To access a HTML control at server side (in your C# code), you need to first add the runat="server" attribute. So, your markup should look like

要在服务器端(在您的 C# 代码中)访问 HTML 控件,您需要首先添加 runat="server" 属性。所以,你的标记应该看起来像

<input type="hidden" id="contentId" runat="server"/>

Now, in the code behind you can use the control by its id contentIditself if the code behind got generated properly.

现在,在后面的代码中,contentId如果后面的代码正确生成,您可以通过其 id本身使用控件。

Please let us know why you are forced to use the FindControl in the first place as it can be accessed by using the id directly.

请让我们知道为什么您必须首先使用 FindControl,因为它可以通过直接使用 id 访问。

Update

更新

As per the comment below, the user for some reason is not interested in making this input a server side control. Then the only possibility by which you can read the values at server side is as below. But this is not advised as any changes to the name goes unnoticed and breaks at runtime.

根据下面的评论,用户出于某种原因对将此输入作为服务器端控件不感兴趣。那么您可以在服务器端读取值的唯一可能性如下。但不建议这样做,因为名称的任何更改都不会引起注意并在运行时中断。

<input type="hidden" id="contentId" name="contentName" runat="server"/>

In Code

在代码中

this.Request.Forms["contentName"]would return the hidden value.

this.Request.Forms["contentName"]将返回隐藏值。

回答by Vijaychandar

Use this code:

使用此代码:

string s=((HiddenField)Panel1.FindControl("contentId")).Value;

string s=((HiddenField)Panel1.FindControl("contentId")).Value;

Here panel is the container control. This may be a grid or anything else or even a master page. But if you are using FindControl, i think the control may be inside some container.

这里的面板是容器控件。这可能是一个网格或其他任何东西,甚至是一个母版页。但是如果您使用 FindControl,我认为该控件可能位于某个容器内。

回答by HatSoft

Try to search it on the page this way

尝试以这种方式在页面上搜索它

HiddenField hf = (HiddenField)page.FindControl("contentId");

回答by Alex Turnbull

To get the value:

获取值:

HiddenField h = (HiddenField)Gridview.FindControl("HiddenFieldName");

Then with that you can put it into a string, if you wish to.

然后,如果您愿意,您可以将其放入字符串中。

回答by daniel

HtmlInputHidden hf = Page.FindControl("contentId") as HtmlInputHidden;
string hfValue = hf.Value;