ASP.Net 3.5: Specifying a control's HTML ID

て烟熏妆下的殇ゞ 提交于 2019-12-06 13:13:43

You may add with code:

MyControl control = new MyControl();
control.ID == "myControl" + count.ToString();
ph.Controls.Add(control);

where ph - Placeholder or Panel control, count - some counter.

Better to add the control from code behind, add a panel and then add controls to it.

Sadly, you're stuck with the .NET id at this time in ASP.NET 3.5. Unless you want to add the control programatically at run time, you can only Read the Id as it will appear on the client. You often have to do some client-side tricks to find the right control.

Once to make a form 508 compliant I had to analyze the pattern that .NET seemed to use to give id's to my nested controls and try to predict it. It seems to have worked for the past year; I keep my fingers crossed. Or, you can get the ClientID property at run time and write it to a hidden field, like so:

<input id='SpecialControlID' type='hidden' value='<%= SpecialControl.ClientID %>'>

Then you javascript can determine the ID. In short, there no good ways, only annoying hack ways. Easiest to add your controls at run-time.

Hope comes with VS2010, where you have options for improving this. You can change the settings for a control to use a specified ID, or to just simplify the algorithm .NET uses to determine the id, so that it is more predictable. Here's a good link.

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