验证失败后如何从日历控件中重新选择日期?
我在用户控件内有一个文本框、一个图像按钮和一个日历控件,用于从日历中选择日期并在文本框中设置所选日期。除了验证之外,一切都运行良好。我尝试验证文本框的值是否为有效日期。如果它不是有效日期,我想从日历中重新选择日期,但看起来如果验证失败,如果我没有在文本框中输入正确的日期,我将无法重新选择日期。基本上,验证要求我在尝试选择有效日期之前先修复无效日期,但我想在验证失败时重新选择。
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="width: 400px">
<asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox>
<asp:ImageButton ID="ImgButton1" runat="server" ImageUrl="~/Images/cal.gif" OnClick="ImgButton1_Click" />
<asp:Calendar ID="Calendar1" runat="server" DayNameFormat="FirstLetter" Width="90px" Font-Names="Arial" Font-Size="11px" NextMonthText="»" PrevMonthText="«" SelectionMode="DayWeekMonth" SelectMonthText="»" SelectWeekText="›" CssClass="myCalendar" BorderStyle="None" CellPadding="1" OnSelectionChanged="Calendar1_SelectionChanged" Visible="False">
<OtherMonthDayStyle ForeColor="Gray" />
<DayStyle CssClass="myCalendarDay" />
<SelectedDayStyle Font-Bold="True" Font-Size="12px" />
<SelectorStyle CssClass="myCalendarSelector" />
<NextPrevStyle CssClass="myCalendarNextPrev" />
<TitleStyle CssClass="myCalendarTitle" />
</asp:Calendar>
<asp:CustomValidator ID="cusValidator1" runat="server" OnServerValidate="Customer_Validation" />
<asp:Label ID="lblError" Text="invalid input" Visible="false" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{ // Set Date Time value into the TextBox control
TextBox1.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");
// Hide the Calendar control after selecting the date
Calendar1.Visible = false;
}
protected void ImgButton1_Click(object sender, EventArgs e)
{
Calendar1.Visible = !Calendar1.Visible;
}
I have a textbox, an imagebutton, and a calendar control inside a user control to pick the date from calendar and set the selected date inside textbox. All works well except the validation. I tried to validate the textbox's value to be a valid date. If it is not a valid date, I want to reselect a date from the calendar, but it looks like if validation failed, I could not reselect date if I did not put a correct date inside the textbox. Basically, validation requires me to fix the invalid date first before I try to select a valid date, but I want to reselect when validation failed.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="width: 400px">
<asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox>
<asp:ImageButton ID="ImgButton1" runat="server" ImageUrl="~/Images/cal.gif" OnClick="ImgButton1_Click" />
<asp:Calendar ID="Calendar1" runat="server" DayNameFormat="FirstLetter" Width="90px" Font-Names="Arial" Font-Size="11px" NextMonthText="»" PrevMonthText="«" SelectionMode="DayWeekMonth" SelectMonthText="»" SelectWeekText="›" CssClass="myCalendar" BorderStyle="None" CellPadding="1" OnSelectionChanged="Calendar1_SelectionChanged" Visible="False">
<OtherMonthDayStyle ForeColor="Gray" />
<DayStyle CssClass="myCalendarDay" />
<SelectedDayStyle Font-Bold="True" Font-Size="12px" />
<SelectorStyle CssClass="myCalendarSelector" />
<NextPrevStyle CssClass="myCalendarNextPrev" />
<TitleStyle CssClass="myCalendarTitle" />
</asp:Calendar>
<asp:CustomValidator ID="cusValidator1" runat="server" OnServerValidate="Customer_Validation" />
<asp:Label ID="lblError" Text="invalid input" Visible="false" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{ // Set Date Time value into the TextBox control
TextBox1.Text = Calendar1.SelectedDate.ToString("MM/dd/yyyy");
// Hide the Calendar control after selecting the date
Calendar1.Visible = false;
}
protected void ImgButton1_Click(object sender, EventArgs e)
{
Calendar1.Visible = !Calendar1.Visible;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实我想通了这一点。通过仅将验证组添加到文本框和所需的验证器,而不是日历控件。即使文本框值无效,它也将允许我从日历控件中重新选择。
Acutally I figured this one out. By adding validation group to only textbox and required validators, not the calendar control. It will allow me to reselect from calendar control even if textbox value is not valid.
这已经解决了。只需设置不同的验证组名称,一切就可以正常工作。
This is solved. Just by setting different validation group name, everything is working fine.