为什么findcontrol(“x”)ASP.NET方法找不到通过javascript制作的HTML控件? [英] Why findcontrol("x") ASP.NET method can not find a HTML control that made via javascript?
问题描述
嗨所有
我的网页上有一个可选联系人,用户可以为手机或电子邮件等任何联系人添加输入文字,当用户点击创建联系人按钮时,它会生成一个输入框属性如唯一ID(例如txtrelat1)或runat =server(在客户端但网页上有一个默认输入框id =txtrelat0)当用户点击提交按钮时,调用register方法在服务器端通过onserverclick事件,
主要问题是,当我使用form1.FindControl(txtrelat 0 )时,它找到并且我可以将它转换为HtmlInputText但是当我想要form1.FindControl(txtrelat 1 )时,它找不到并返回null?
谢谢所有
我尝试过:
HtmlInputText intex = FindControl(txtrelat0)为HtmlInputText;
生成页面时,.net处理runat标记,它没有客户端的意义,并添加了一个带有runat标记的元素从javascript不会使它成为服务器端控件,它只是创建一个具有被忽略属性的元素。
你要做的就是确保输入你创建了一个唯一的名称(id是无关紧要的),并在你的服务器代码中使用Request.Form来读取值。因此,创建像
< input type =textname =text1/>
< input type =textname =text2/>
然后在您的服务器代码< br $>
Request.Form [ text1 中跨度>];
Request.Form [ text2];
hi all
my web page has an optional contact that user can add input text for any contact like cellphone or email,when user click create contact button, it make a input box with attributes like unique id(for example "txtrelat1") or runat="server"(in client-side but web page has an input box 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
What I have tried:
HtmlInputText intex = FindControl("txtrelat0") as HtmlInputText;
The runat tag is processed by .net when the page is generated, it has no meaning client-side and adding an element with a runat tag from javascript doesn't make that a server-side control, it simply creates an element with an attribute that is ignored.
What you have to do is ensure the input you create has a unique name (the id is irrelevant) and in your server code use Request.Form to read the value. So create boxes like
<input type="text" name="text1"/> <input type="text" name="text2"/>
then in your server-code
Request.Form["text1"]; Request.Form["text2"];
这篇关于为什么findcontrol(“x”)ASP.NET方法找不到通过javascript制作的HTML控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!