ASP.Net: ClientID not correct in code-behind of a user control

独自空忆成欢 提交于 2019-12-05 08:02:44

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.

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.

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!