J
jmclej
Here is what I have :
<asp:templatecolumn HeaderText="HrBegin">
<itemtemplate>
<asp:Label id="lblHrBegin" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
</asp:label>
</itemtemplate>
<footertemplate>
<asp:textbox id="tbHrBeginInsert" runat="server"
Width="100%"></asp:textbox>
<asp:CustomValidator id="Customvalidator1"
ControlToValidate="tbHrBeginInsert" OnServerValidate="ServerValidation"
Display="None" runat="server"></asp:CustomValidator>
</footertemplate>
<edititemtemplate>
<asp:textbox id="tbHrBegin" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.HrBegin") %>'
Width="100%"></asp:textbox>
<asp:CustomValidator id="Customvalidator2"
ControlToValidate="tbHrBegin" OnServerValidate="ServerValidation"
Display="None" runat="server"></asp:CustomValidator>
</edititemtemplate>
</asp:templatecolumn>
Code Behind:
void ServerValidation(object source, ServerValidateEventArgs args) {
try
{
Regex r = new
Regex("(([0-1][0-9])|(2[0-3]))[0-5][0-9])[0-5][0-9])"); //Only
allow "02:03:04"
Match m = r.Match(args.Value);
// Test whether the value entered into the text box is in a
correct
hour format.
if (m.Success)
args.IsValid = true;
else
args.IsValid = false;
}
catch(Exception ex)
{
args.IsValid = false;
}
}
I put the ServerValidation() function on the code behind because I
thought that this was the way of doing server-side validation. But it
actually works only if I put this function inside a script block on the
aspx page, which looks like then client-side validation, no? Maybe I
need to add something in the InitializeComponent() method?
But anyway, even like this there is still something that is not good.
It's that I get all the time the same text to be validated, that is the
content of my itemtemplate. What should I do to make ServerValidation()
to test the new data entered in the textbox tbHrBeginInsert or
tbHrBegin?
Thank you very much for your help.
JULIEN
<asp:templatecolumn HeaderText="HrBegin">
<itemtemplate>
<asp:Label id="lblHrBegin" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
</asp:label>
</itemtemplate>
<footertemplate>
<asp:textbox id="tbHrBeginInsert" runat="server"
Width="100%"></asp:textbox>
<asp:CustomValidator id="Customvalidator1"
ControlToValidate="tbHrBeginInsert" OnServerValidate="ServerValidation"
Display="None" runat="server"></asp:CustomValidator>
</footertemplate>
<edititemtemplate>
<asp:textbox id="tbHrBegin" runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.HrBegin") %>'
Width="100%"></asp:textbox>
<asp:CustomValidator id="Customvalidator2"
ControlToValidate="tbHrBegin" OnServerValidate="ServerValidation"
Display="None" runat="server"></asp:CustomValidator>
</edititemtemplate>
</asp:templatecolumn>
Code Behind:
void ServerValidation(object source, ServerValidateEventArgs args) {
try
{
Regex r = new
Regex("(([0-1][0-9])|(2[0-3]))[0-5][0-9])[0-5][0-9])"); //Only
allow "02:03:04"
Match m = r.Match(args.Value);
// Test whether the value entered into the text box is in a
correct
hour format.
if (m.Success)
args.IsValid = true;
else
args.IsValid = false;
}
catch(Exception ex)
{
args.IsValid = false;
}
}
I put the ServerValidation() function on the code behind because I
thought that this was the way of doing server-side validation. But it
actually works only if I put this function inside a script block on the
aspx page, which looks like then client-side validation, no? Maybe I
need to add something in the InitializeComponent() method?
But anyway, even like this there is still something that is not good.
It's that I get all the time the same text to be validated, that is the
content of my itemtemplate. What should I do to make ServerValidation()
to test the new data entered in the textbox tbHrBeginInsert or
tbHrBegin?
Thank you very much for your help.
JULIEN