ASP.Net:用户控件的代码隐藏中的 ClientID 不正确
下面的代码不起作用。标记位于用户控件中,我想这就是 ClientID 返回 TextBox id 的错误前缀的原因。
标记:
<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">
代码隐藏:
btnFind.Attributes.Add("onClick",string.Format("DoctorLink
('{0}',document.getElementById('{1}').value,{2});",
row["ZipCode"],
txtName.ClientID));
浏览器中的结果:
<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);" />
如您所见,JavaScript 调用的参数是 DoctorsMainArea1_ctl00_txtName
,但输入元素的实际 id 是 DoctorsMainArea1_ctl01_txtName
。
知道如何解决这个问题吗? jQuery?我对正在发生的事情的解释不太感兴趣(也许此页面上有另一个控件正在干扰),但更感兴趣的是解决问题的更强大的方法。
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.
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
.
Any idea how to fix this? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道您使用的是哪个 asp.net 版本,但在 4.0 中,您可以在任何服务器控件中声明 ClientIDMode="static",它将在浏览器中为您提供确切的 id。
示例:
其他是可预测的、继承的,并且可以与 ClientIdRowsuffix 一起使用。可以在页面级别甚至母版页甚至 web.config 文件中使用。
web.config 文件示例:
在 tekpub 观看了 Craig Shoemaker 的视频,您还可以在 Rick 的博客链接文本。这很酷。
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.
Example:
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.
Example on web.config file:
Watched Craig shoemaker's Video at tekpub, you can also read more about it at Rick's bloglink text. It's pretty cool tho.
您应该尝试移动将 onclick 属性添加到页面或用户控件中 PreRender 事件(或 OnPreRender 重写)中的按钮的代码。这应该可以得到正确的 ClientID。
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. That should probably get the ClientID right.
快速解决方案:
发生这种情况是因为您的页面某处有内容占位符。
A fast solution:
This happens because you have a content placeholder in your page somewhere.
另一个解决方案:
html标签:
code-bind:
即可获取该值。
并且您只需使用html控件
another solution:
html tag:
code-bind:
and you can get the value
just use the html control.