Problem With Custom Validation In a Details View

  • Thread starter Microsoft News Group
  • Start date
M

Microsoft News Group

I am trying to validate two textboxes as their values are the same, or both
are null do not insert the data. I do not just want one to try to be null,
I want both of them, so the requirevalidator will not work because I want
both to be null or equal. I am assuming using the Custom Validator. Here
is my DetailsViewCode:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="PriceRangeID"

DataSourceID="PriceRangeDataSource" DefaultMode="Insert" >

<Fields>

<asp:TemplateField HeaderText="Min" SortExpression="Min">

<EditItemTemplate>

&nbsp;<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Min")
%>'></asp:TextBox>

<asp:CustomValidator ID="EmptyInsertSpacer" runat="server"
OnServerValidate="EmptyInsertSpacer_ServerValidate"
ControlToValidate="TextBox1"
ValidationGroup="InsertValidatorGroup"></asp:CustomValidator>&nbsp;

</EditItemTemplate>

<InsertItemTemplate>

<asp:TextBox ID="MinTextBox" runat="server" Text='<%# Bind("Min")
%>'></asp:TextBox>

<asp:CompareValidator ID="MinValidator" runat="server"
ControlToValidate="MinTextBox"

ErrorMessage="Minimum value is not a currency value!" Type="Currency"
ValidationGroup="InsertValidatorGroup"
Operator="DataTypeCheck">*</asp:CompareValidator>

</InsertItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Bind("Min") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Max" SortExpression="Max">

<EditItemTemplate>

<asp:TextBox ID="MaxInsertTextBox" runat="server" Text='<%# Bind("Max")
%>'></asp:TextBox>&nbsp;

</EditItemTemplate>

<InsertItemTemplate>

<asp:TextBox ID="MaxTextBox" runat="server" Text='<%# Bind("Max")
%>'></asp:TextBox>

<asp:CompareValidator ID="MaxValidator" runat="server"
ControlToValidate="MaxTextBox"

ErrorMessage="Maximum value entered is not a curreny value!"
Operator="DataTypeCheck"

Type="Currency"
ValidationGroup="InsertValidatorGroup">*</asp:CompareValidator>

</InsertItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Max") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:CommandField ShowInsertButton="True"
ValidationGroup="InsertValidatorGroup" />

</Fields>

</asp:DetailsView>

Here is my custom validator code which is not working. Any help would be
greatly appreciated.



protected void EmptyInsertSpacer_ServerValidate(object source,
ServerValidateEventArgs args)

{

DetailsViewRow rowmin = DetailsView1.Rows[1];

string min = rowmin.Cells[0].Text;

DetailsViewRow rowmax = DetailsView1.Rows[1];

string max = rowmax.Cells[1].Text;

if (min == max)

{

args.IsValid = false;

}

else

{

args.IsValid = true;

}


}
 
M

Microsoft News Group

Any help here?

Microsoft News Group said:
I am trying to validate two textboxes as their values are the same, or both
are null do not insert the data. I do not just want one to try to be null,
I want both of them, so the requirevalidator will not work because I want
both to be null or equal. I am assuming using the Custom Validator. Here
is my DetailsViewCode:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="PriceRangeID"

DataSourceID="PriceRangeDataSource" DefaultMode="Insert" >

<Fields>

<asp:TemplateField HeaderText="Min" SortExpression="Min">

<EditItemTemplate>

&nbsp;<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Min")
%>'></asp:TextBox>

<asp:CustomValidator ID="EmptyInsertSpacer" runat="server"
OnServerValidate="EmptyInsertSpacer_ServerValidate"
ControlToValidate="TextBox1"
ValidationGroup="InsertValidatorGroup"></asp:CustomValidator>&nbsp;

</EditItemTemplate>

<InsertItemTemplate>

<asp:TextBox ID="MinTextBox" runat="server" Text='<%# Bind("Min")
%>'></asp:TextBox>

<asp:CompareValidator ID="MinValidator" runat="server"
ControlToValidate="MinTextBox"

ErrorMessage="Minimum value is not a currency value!" Type="Currency"
ValidationGroup="InsertValidatorGroup"
Operator="DataTypeCheck">*</asp:CompareValidator>

</InsertItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Bind("Min")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Max" SortExpression="Max">

<EditItemTemplate>

<asp:TextBox ID="MaxInsertTextBox" runat="server" Text='<%# Bind("Max")
%>'></asp:TextBox>&nbsp;

</EditItemTemplate>

<InsertItemTemplate>

<asp:TextBox ID="MaxTextBox" runat="server" Text='<%# Bind("Max")
%>'></asp:TextBox>

<asp:CompareValidator ID="MaxValidator" runat="server"
ControlToValidate="MaxTextBox"

ErrorMessage="Maximum value entered is not a curreny value!"
Operator="DataTypeCheck"

Type="Currency"
ValidationGroup="InsertValidatorGroup">*</asp:CompareValidator>

</InsertItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Max")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:CommandField ShowInsertButton="True"
ValidationGroup="InsertValidatorGroup" />

</Fields>

</asp:DetailsView>

Here is my custom validator code which is not working. Any help would be
greatly appreciated.



protected void EmptyInsertSpacer_ServerValidate(object source,
ServerValidateEventArgs args)

{

DetailsViewRow rowmin = DetailsView1.Rows[1];

string min = rowmin.Cells[0].Text;

DetailsViewRow rowmax = DetailsView1.Rows[1];

string max = rowmax.Cells[1].Text;

if (min == max)

{

args.IsValid = false;

}

else

{

args.IsValid = true;

}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top