user1292656
user1292656

Reputation: 2560

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

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:

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

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

Upvotes: 3

Views: 24787

Answers (5)

daniel
daniel

Reputation: 155

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

Upvotes: 1

Alex Turnbull
Alex Turnbull

Reputation: 21

To get the value:

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

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

Upvotes: 2

Ramesh
Ramesh

Reputation: 13266

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

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

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

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.

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.

Upvotes: 6

HatSoft
HatSoft

Reputation: 11191

Try to search it on the page this way

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

Upvotes: 2

Vijaychandar
Vijaychandar

Reputation: 714

Use this code:

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.

Upvotes: 1

Related Questions