下面的代码无法正常工作。该标记是在用户控制,我想这就是为什么客户端ID返回文本框的ID错误的前缀
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.
标记:
<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);" />
正如你可以看到,JavaScript调用的参数是 DoctorsMainArea1_ctl00_txtName ,但输入元素的实际编号为 DoctorsMainArea1_ctl01_txtName 。
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.
任何想法如何解决这个? jQuery的?我不是那么这是怎么回事的解释很感兴趣(也许有这个页面被干扰另一个控制),但解决问题的更可靠的方法。
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.
推荐答案您应该尝试在你的页面或用户控制移动,增加了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.
更多推荐
ASP.Net:在的ClientID用户控件的代码隐藏不正确
发布评论