Answered by:
How to dynamically set a MaskedEditExtender and MaskedEditValidator to a TextBox

Question
-
User618007423 posted
I have a TextBox that is used for user input when searching. I have a DropDownList that is used to indicate what field to search. When the user selects Date from the DropDownList I would like to set a MaskedEditExtender and MaskedEditValidator to the TextBox for date entry.
Does anyone know how to do this? I tried setting the TargetControlID="" and ControlToValidate="" initially and then set these properties when the user selects Date from the DropDownList, but it did not like that.
Thanks in advance.
Friday, June 27, 2014 5:02 PM
Answers
-
User-417640953 posted
Hi Drew1755,
Thank you post the issue to asp.net forum.
If you want to apply the MaskedEditExtender to the TextBox when the dropdownlist select a "Date" value, I suggest you
change the TargetControlID of MaskedEditExtender in the code behind. However, at first we should set another TextBox
as the TargetControlID of MaskedEditExtender, and it is not visiable like below demo.
<div> <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" CssClass="MyClass"></asp:TextBox> <style> .MyClass { display:none; } </style> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Text="string" Value="string"></asp:ListItem> <asp:ListItem Text="Date" Value="Date"></asp:ListItem> </asp:DropDownList> <asp:MaskedEditExtender ID="MasqueEditionExtenseurDateOBS" runat="server" TargetControlID="TextBox2" MaskType="Date" Mask="99/99/9999" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError" InputDirection="LeftToRight" ErrorTooltipEnabled="true" /> <asp:MaskedEditValidator ID="MasqueEditionValidationDateOBS" runat="server" Display="None" ControlExtender="MasqueEditionExtenseurDateOBS" ControlToValidate="TextBox2" SetFocusOnError="false" IsValidEmpty="True" EmptyValueMessage="Veuillez saisir une date." ValidationGroup="Envoyer" MinimumValue="01/01/0001" MaximumValueMessage="La date d'installation ne peut être inférieure à 8 semaines ou 15 jours ouvrés suivant vos choix." /> </div>
code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedValue == "Date") { //change the TargetControlID MasqueEditionExtenseurDateOBS.TargetControlID = TextBox1.ID; } //this is necessary TextBox1.Text = ""; }
Hope this helps, thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 30, 2014 3:49 AM
All replies
-
User-1644933537 posted
I have never used that extender, but have you tried disabling then enabling it when needed?
Friday, June 27, 2014 8:06 PM -
User555306248 posted
http://forums.asp.net/t/1418013.aspx?Error+in+adding+MaskedEdit+dynamically+
Monday, June 30, 2014 12:26 AM -
User-417640953 posted
Hi Drew1755,
Thank you post the issue to asp.net forum.
If you want to apply the MaskedEditExtender to the TextBox when the dropdownlist select a "Date" value, I suggest you
change the TargetControlID of MaskedEditExtender in the code behind. However, at first we should set another TextBox
as the TargetControlID of MaskedEditExtender, and it is not visiable like below demo.
<div> <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" CssClass="MyClass"></asp:TextBox> <style> .MyClass { display:none; } </style> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Text="string" Value="string"></asp:ListItem> <asp:ListItem Text="Date" Value="Date"></asp:ListItem> </asp:DropDownList> <asp:MaskedEditExtender ID="MasqueEditionExtenseurDateOBS" runat="server" TargetControlID="TextBox2" MaskType="Date" Mask="99/99/9999" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError" InputDirection="LeftToRight" ErrorTooltipEnabled="true" /> <asp:MaskedEditValidator ID="MasqueEditionValidationDateOBS" runat="server" Display="None" ControlExtender="MasqueEditionExtenseurDateOBS" ControlToValidate="TextBox2" SetFocusOnError="false" IsValidEmpty="True" EmptyValueMessage="Veuillez saisir une date." ValidationGroup="Envoyer" MinimumValue="01/01/0001" MaximumValueMessage="La date d'installation ne peut être inférieure à 8 semaines ou 15 jours ouvrés suivant vos choix." /> </div>
code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedValue == "Date") { //change the TargetControlID MasqueEditionExtenseurDateOBS.TargetControlID = TextBox1.ID; } //this is necessary TextBox1.Text = ""; }
Hope this helps, thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 30, 2014 3:49 AM