微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 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的?我对解释正在发生的事情并不感兴趣(可能在此页面上有另一个控件干扰),但是解决问题的方法更为强大.

解决方法

您应该尝试将添加onclick属性代码移动到页面用户控件中PreRender事件(或OnPreRender覆盖)中的按钮.这应该可以使ClientID正确.

原文地址:https://www.jb51.cc/csharp/243083.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐