ASP.Net:用户控件的代码隐藏中的 ClientID 不正确

发布于 2024-09-15 17:03:17 字数 1197 浏览 1 评论 0原文

下面的代码不起作用。标记位于用户控件中,我想这就是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迟月 2024-09-22 17:03:17

我不知道您使用的是哪个 asp.net 版本,但在 4.0 中,您可以在任何服务器控件中声明 ClientIDMode="static",它将在浏览器中为您提供确切的 id。

示例:

<asp:Textbox id="txtName" runat="server" ClientIdMode="static"/>

其他是可预测的、继承的,并且可以与 ClientIdRowsuffix 一起使用。可以在页面级别甚至母版页甚至 web.config 文件中使用。

web.config 文件示例:

<system.web>
<Pages clientIDMode="predictable"/>
other system web properties
</system.web>

在 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:

<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.

Example on web.config file:

<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 bloglink text. It's pretty cool tho.

软甜啾 2024-09-22 17:03:17

您应该尝试移动将 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.

夏夜暖风 2024-09-22 17:03:17

快速解决方案:

btnFind.Attributes.Add("onClick",string.Format("DoctorLink
        ('{0}',document.getElementById('{1}').value,{2});",
        row["ZipCode"],
        "DoctorsMainArea1_ctl01_" + txtName.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.

猥琐帝 2024-09-22 17:03:17

另一个解决方案:
html标签:

<input type="text" name="txtName" id="txtName" />

code-bind:

string txtName_value = Request.Forms["txtName"];

即可获取该值。

并且您只需使用html控件

another solution:
html tag:

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文