my web page has an optional contact panel that user can add input text for any contact, like cellphone or email,when user click create contact button, it make an input text with attributes like unique id(for example txtrelat1
) or runat="server"
(in client-side but web page has an input text with id="txtrelat0"
by default) and when user click submit button ,calls register
method in server-side by onserverclick event,
main question is,when i use form1.FindControl("txtrelat0")
,it's find and i can convert it to HtmlInputText
but when i want form1.FindControl("txtrelat1")
it's not find and return null?
thank's all

- 16,571
- 12
- 101
- 98

- 11
- 3
-
You cannot find JavaScript controls via code behind. – INDIA IT TECH Mar 26 '16 at 12:03
-
1Simply adding the attribute `runat="server"` from the client side doesn't mean that it's going to get picked up. If you're looking for posted values from the client side, you should be able to dig them out of the `Request.Forms` property via the `name` attribute of your text element. – mason Mar 26 '16 at 13:44
1 Answers
FindControl
finds server controls in the page or control currently executing. It deals only with server controls. The code behind can still read values posted from the inputs you add. You could call Request.Form["x"]
.
The downside is that if you add additional controls to the page and then do a postback the server page isn't going to "remember" the elements you added. They'll just disappear after the postback.
Mixing those types of client/server behaviors with webforms isn't fun. You might find it easier to do it with all server controls. Just set Visible="false"
or "true"
in your server code in response to server events. Or you could do it all on the client using API calls.
Even though I don't like webforms that much, when I have to work in a webforms project it's often easier to do things the webforms way with server controls.

- 27,588
- 3
- 45
- 62