BumbleBee
BumbleBee

Reputation: 10789

Change the ControlToValidate of RequiredFieldValidator dynamically

I have a textbox and dropdown inside a <td> based on certain condition I'll have to hide the text box and show the dropdown. If dropdown is visible is it possible to use the same requiredfield Validator. Is this possible?

<td>
    <asp:TextBox ID="txtLimit" runat="server" />
    <asp:DropDownList ID="ddlCurLiabiltyLimits" runat="server" CssClass="TextNormal" OnSelectedIndexChanged="ddlCurLiabiltyLimits_SelectedIndexChanged">  
    </asp:DropDownList>
    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtLimit" ErrorMessage="Answer is required." ClientIDMode="Static" />
</td>

In my code behind :

RequiredFieldValidator1.ControlToValidate = ddlCurLiabiltyLimits.ID ;

but this doesn't seem to be working.

Upvotes: 2

Views: 3067

Answers (2)

pseudocoder
pseudocoder

Reputation: 4392

One thing that might be causing you problems is when using RequiredFieldValidator on a DropDownList, you will want to specify the RequiredFieldValidator.InitialValue property to indicate the Value of the "first" item in the DropDownList. Maybe that is where your problem is?

Upvotes: 2

Dr. Wily&#39;s Apprentice
Dr. Wily&#39;s Apprentice

Reputation: 10280

My guess is that you might be assigning the ID to the ControlToValidate property too late in the page lifecycle. Is it possible to assign this during the page's OnInit event?

It might be easier to just use a second RequiredFieldValidator for the DropDownList.

Upvotes: 2

Related Questions