<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ed Giardina's .NET Blog : ASP.NET, Entity Field Templates</title><link>http://bloggingabout.net/blogs/egiardina/archive/tags/ASP.NET/Entity+Field+Templates/default.aspx</link><description>Tags: ASP.NET, Entity Field Templates</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>ASP.NET Dynamic Data, adding 'Required Field' asterisks or other notation to control labels</title><link>http://bloggingabout.net/blogs/egiardina/archive/2011/04/25/asp-net-dynamic-data-adding-required-field-asterisks-or-other-notation-to-control-labels.aspx</link><pubDate>Mon, 25 Apr 2011 22:05:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:486393</guid><dc:creator>Richthofen</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/egiardina/rsscomments.aspx?PostID=486393</wfw:commentRss><comments>http://bloggingabout.net/blogs/egiardina/archive/2011/04/25/asp-net-dynamic-data-adding-required-field-asterisks-or-other-notation-to-control-labels.aspx#comments</comments><description>&lt;p&gt;So this is a pretty straightforward concern, but I wanted to add validation notation to a FieldTemplate&amp;#39;s Label, and not necessarily its control. In ASP.NET&amp;#39;s dynamic data, this isn&amp;#39;t straightforward. What I found is that using Entity Templates is better than editing individual Field Templates.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Go into your dynamic data directory and open&amp;nbsp;Default_Edit.ascx and&amp;nbsp;Default_Insert.ascx files. Add a Label to each, like so. This label represents our required field token.&lt;/p&gt;
&lt;p&gt;&amp;lt;asp:Label OnInit=&amp;quot;Required_Init&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;quot; *&amp;quot; Visible=&amp;quot;false&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;Now, you&amp;#39;ll notice this control has an OnInit Handler. Basically, these controls IDs are not available in the codebehind based on the draw order, from what I can tell. So you can set their values when they are drawn, not on a global control-wide event, but on the individual control&amp;#39;s events.&lt;/p&gt;
&lt;pre&gt;protected void Required_Init(object sender, EventArgs e)
	{

	if (currentColumn.TypeCode != TypeCode.Boolean)
			((Label)sender).Visible = currentColumn.IsRequired;
	}
&lt;/pre&gt;
&lt;p&gt;Basically, you create a simple event listener that will hide or show the control you just added based on the required-ness of the field. Note that I do not set the label to visible if the column is bound to a Boolean; this is because Booleans render as checkboxes and the idea of &amp;#39;requiredness&amp;#39; sort of doesn&amp;#39;t fit... if you don&amp;#39;t check the checkbox, you are still submitting the field with a value of &amp;#39;false&amp;#39;, so required does not apply.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=486393" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/egiardina/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://bloggingabout.net/blogs/egiardina/archive/tags/Dynamic+Data/default.aspx">Dynamic Data</category><category domain="http://bloggingabout.net/blogs/egiardina/archive/tags/Entity+Field+Templates/default.aspx">Entity Field Templates</category><category domain="http://bloggingabout.net/blogs/egiardina/archive/tags/Required+Fields/default.aspx">Required Fields</category></item></channel></rss>