开发者

Master Pages Controls Id After Rendered

In my asp.net projects there is a master page and t开发者_如何学运维here is a control on Contentplaceholder1. Changing this control's id after rendered like "ctl00_Contentplaceholder1_control" in my local. But at the server its changing like "Contentplaceholder1_control". When i used documents.getelelentbyid('ctl00_Contentplaceholder1_control') js function, its working on my local but not working at the server. So why disappearing "ctl00_" when app runs at the server?

Edit:

Why doesn't the server side control id contains "ctl00_" after rendered?


Use the server side property ClientID to get the appropriate ID or if you are using .NET 4, use ClientIDMode.


Why don't you use ClientIDMode to make static IDs? Which version of .NET Framework are you using?

Another options (for older versions) is to wrap your .NET control in a div with static id, then use jQuery like this:

 var wrapper = $('#wrapperDivId');
 var control = wrapper.find('typeOfControl')[0];

This way, no matter of how your control's id is rendered, you can always have access to it.

For example, TextBoxes render to <input type='text' />. Now you can wrap your text box inside a div like this:

 <div id='wrapper'>
      <asp:TextBox ID='firstName' runat='server' />
 </div>

Now, no matter of the client id, you can get your textbox at client side using:

 $('#wrapper input')


You should do like...

documents.getElelentbyid('<%=ControlID.ClientID %>') 


document.getElementById("<%= control.ClientID %>");

that is much safer and better approach than hard-coding client id.


Answer is here.

My hosting server changed their asp.net servers to asp.net 4 server. So my controls rendered without "ctl00_". I compiled my .net 3.5 project to the .net 4 and I changed web.config file like this.

Now its working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜