Asked by:
Code blocks usage

Question
-
User-1765853593 posted
Hello,I'm using template engine similar to master pages but for ASP.NET 1.1.
And I cannot use code blocks there because of when templating is performed an error occurs:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).But I still find code blocks very useful and want to use it, for example to emit HTML elemnt IDs to scripts.
Is there any way to deal with that?
Ragards,
Dmitriy.
Thursday, April 12, 2007 5:00 AM
All replies
-
User945078486 posted
Hello Dmitriy,
This error happens when asp.net encounters code blocks like <%= %>, or <%# %>, in a User Control and the code-behind the User Control is trying to modify the controls collection of the page (usually a LoadControl statement).
Refer to this thread
http://blogs.snapsis.com/PermaLink,guid,381f9036-432e-468c-bc2e-b52ab86f12ea.aspx
http://west-wind.com/WebLog/posts/5758.aspx
hope it helps,
Jessica
Thursday, April 12, 2007 11:43 PM -
User-1765853593 posted
Hello Jessica,
Thank you for very informative links.
But what is the best way in your opinion to emit control's ClientID into scripts with Master Pages?
Cheers,
Dmitriy.
Friday, April 13, 2007 1:53 PM -
User945078486 posted
Hi Dmitriy,
I think you can use
ContentPlaceHolder cp = Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
WebUserControl wb = cp.FindControl("WebUserControl1") as WebUserControl;
TextBox tb = wb.FindControl("TextBox1") as TextBox;
tb.Text = "abc";Hope it helps,
Jessica
Wednesday, April 18, 2007 3:31 AM -
User-1765853593 posted
Hi Jessica,
Yes we always can use code like this.
But the thing is most usefull for me with code bloack is emiting server control's ID just into page like this.Page.aspx
...
<script type="text/javascript">
function someJSFunction()
{
var el = getElementById('<%= SomeServerControl.ClientID %>');
// do something with element
}
</script>...
Cheers,
Dmitriy.Wednesday, April 18, 2007 7:51 AM