[英]ASP.Net: ClientID not correct in code-behind of a user control
The following code does not work. 以下代码不起作用。 The markup is in a User Control and I suppose that's why ClientID returns the wrong prefix for the TextBox id. 标记位于用户控件中,我想这就是ClientID为TextBox id返回错误前缀的原因。
Markup: 标记:
<INPUT id="txtName" runat="server" maxlength="50" style="WIDTH:100px">
<INPUT type="button" value="Find Your Doctor" id="btnFind" runat="server"
style="MARGIN-LEFT:10px;WIDTH:130px">
Code-Behind: 代码隐藏:
btnFind.Attributes.Add("onClick",string.Format("DoctorLink
('{0}',document.getElementById('{1}').value,{2});",
row["ZipCode"],
txtName.ClientID));
Results in browser: 浏览器结果:
<input name="DoctorsMainArea1$ctl01$txtName" type="text"
id="DoctorsMainArea1_ctl01_txtName" maxlength="50" style="WIDTH:100px" />
<input name="DoctorsMainArea1$ctl01$btnFind" type="button"
id="DoctorsMainArea1_ctl01_btnFind" value="Find Your Doctor" style="MARGIN-
LEFT:10px;WIDTH:130px" onClick="PrepareDoctorLink('90210',
document.getElementById('DoctorsMainArea1_ctl00_txtName').value);" />
As you can see, the parameter for the JavaScript call is DoctorsMainArea1_ctl00_txtName
, but the actual id of the input element is DoctorsMainArea1_ctl01_txtName
. 如您所见,JavaScript调用的参数是DoctorsMainArea1_ctl00_txtName
,但输入元素的实际ID是DoctorsMainArea1_ctl01_txtName
。
Any idea how to fix this? 知道如何解决这个问题吗? jQuery? jQuery的? I am not so much interested in an explanation of what's going on (maybe there is another control on this page that is interfering), but a more robust way to solve the problem. 我对解释正在发生的事情并不感兴趣(可能在此页面上有另一个控件干扰),但是解决问题的方法更为强大。
I don't know which asp.net version you are using but in 4.0 you can declare inside any server control ClientIDMode="static" and it will give you the exact id in browser. 我不知道你使用的是哪个asp.net版本但是在4.0中你可以在任何服务器控件内声明ClientIDMode =“static”,它会在浏览器中为你提供准确的id。
Example: 例:
<asp:Textbox id="txtName" runat="server" ClientIdMode="static"/>
Others are predictable, inherit and it can be used with ClientIdRowsuffix.Can be used at page level and even on master pages and even in web.config file. 其他是可预测的,继承的,它可以与ClientIdRowsuffix一起使用。可以在页面级别甚至在母版页甚至web.config文件中使用。
Example on web.config file: web.config文件的示例:
<system.web>
<Pages clientIDMode="predictable"/>
other system web properties
</system.web>
Watched Craig shoemaker's Video at tekpub, you can also read more about it at Rick's blog link text . 在tekpub观看了Craig鞋匠的视频,您还可以在Rick的博客链接文本中阅读更多相关信息。 It's pretty cool tho. 这很酷。
You should try moving the code that adds the onclick attribute to the button in the PreRender event (or OnPreRender override) in your page or user-control. 您应该尝试将添加onclick属性的代码移动到页面或用户控件中PreRender事件(或OnPreRender覆盖)中的按钮。 That should probably get the ClientID right. 这应该可以使ClientID正确。
A fast solution: 快速解决方案:
btnFind.Attributes.Add("onClick",string.Format("DoctorLink
('{0}',document.getElementById('{1}').value,{2});",
row["ZipCode"],
"DoctorsMainArea1_ctl01_" + txtName.ClientID));
This happens because you have a content placeholder in your page somewhere. 这是因为您的页面中有一个内容占位符。
another solution: html tag: 另一种解决方案:html标签:
<input type="text" name="txtName" id="txtName" />
code-bind: 代码绑定:
string txtName_value = Request.Forms["txtName"];
and you can get the value 你可以获得价值
just use the html control. 只需使用html控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.