<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.aspnetzone.de/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>Search results matching tags 'Validation' and 'ValidateEmptyText'</title><link>http://www.aspnetzone.de/search/SearchResults.aspx?o=DateDescending&amp;tag=Validation,ValidateEmptyText&amp;orTags=0</link><description>Search results matching tags 'Validation' and 'ValidateEmptyText'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61120.2)</generator><item><title>CustomValidator - Weshalb bei leeren Werten nicht validiert wird</title><link>http://www.aspnetzone.de/blogs/peterbucher/archive/2007/08/07/customvalidator-leere-werte-werden-nicht-validiert.aspx</link><pubDate>Tue, 07 Aug 2007 20:49:00 GMT</pubDate><guid isPermaLink="false">ce930855-ae9b-4fa4-8077-06a76071cc6a:183548</guid><dc:creator>Peter Bucher</dc:creator><description>&lt;P&gt;Die Validator Controls wurden so konzipiert, das sie kombiniert werden können.&lt;BR&gt;Das heisst dann aber auch, das bei der Überprüfung auf einen leeren Wert &lt;STRONG&gt;oder&lt;/STRONG&gt; falls ein Wert gegeben ist, dieser passen muss, jeweils ein RequiredFieldValidator und ein zusätzlicher Validator auf ein Control angesetzt werden müssten.&lt;/P&gt;
&lt;P&gt;Natürlich kann ein solcher Fall auch mithilfe von einem CustomValidator gelöst werden, jedoch gibt es da eine Stolperfalle.&lt;BR&gt;Kommt ein leerer Wert beim CustomValidator an, gibt er per Default die Prüfung als Erfolg an.&lt;BR&gt;Mit den folgenden Codeausschnitten die aus den Original&amp;nbsp;Validator Controls entnommen sind (&lt;A href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/A&gt;)&amp;nbsp;sollte klar werden, weshalb dann keine weitere Prüfung anspringt:&lt;/P&gt;
&lt;P&gt;RequiredFieldValidator:&lt;BR&gt;&lt;/P&gt;
&lt;DIV class=CodeBox&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;override&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;bool&lt;/span&gt; EvaluateIsValid()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;string&lt;/span&gt; controlValidationValue &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;base&lt;/span&gt;.GetControlValidationValue(&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;base&lt;/span&gt;.ControlToValidate);&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;return&lt;/span&gt; ((controlValidationValue == &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;null&lt;/span&gt;) || !controlValidationValue.Trim().Equals(&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;this&lt;/span&gt;.InitialValue.Trim()));&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CustomValidator:&lt;BR&gt;
&lt;DIV class=CodeBox&gt;&lt;code&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;override&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;bool&lt;/span&gt; EvaluateIsValid()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;string&lt;/span&gt; controlValidationValue &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;string&lt;/span&gt;.Empty;&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;string&lt;/span&gt; name &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;base&lt;/span&gt;.ControlToValidate;&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt; (name.Length &amp;gt; 0)&lt;br /&gt;    {&lt;br /&gt;        controlValidationValue &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;=&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;base&lt;/span&gt;.GetControlValidationValue(name);&lt;br /&gt;        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;if&lt;/span&gt; (((controlValidationValue == &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;null&lt;/span&gt;) || (controlValidationValue.Trim().Length == 0)) &amp;amp;&amp;amp; !&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;this&lt;/span&gt;.ValidateEmptyText)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;return&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;true&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;return&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 14px;font-weight: normal;"&gt;this&lt;/span&gt;.OnServerValidate(controlValidationValue);&lt;br /&gt;}&lt;/span&gt;&lt;/code&gt; &lt;/DIV&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Der CustomValidator stosst nur eine Prüfung an, falls ValidateEmptyText auf false steht.&lt;BR&gt;Die Lösung ist in dem Falle völlig simpel, einfach die Eigenschaft &lt;A href="http://msdn2.microsoft.com/de-de/library/system.web.ui.webcontrols.customvalidator.validateemptytext(VS.80).aspx"&gt;ValidateEmptyText&lt;/A&gt; auf true setzen.&lt;/P&gt;</description></item></channel></rss>