Custom validator inside DataGrid

S

Stephan Bour

Hi,
I need to validate a text box in a datagrid nested inside a datalist. All I
need is to validate that any text is entered in the textbox. However, a
requiredfieldvalidator would not do because I want to perform the validation
only on the datagrid rows where a check box is checked.

Part of the datagrid:

<asp:TemplateColumn HeaderText="C #">
<ItemTemplate>
<asp:TextBox Width="50" MaxLenght="5" TextMode="SingleLine" ID="Cnumber"
runat="server" />
<asp:CustomValidator id="CNumNotNull"
ControlToValidate="Cnumber"
Display="Static"
InitialValue=""
OnServerValidate = "Validate"
ErrorMessage="Please enter a C Number"
runat="server">
*
</asp:CustomValidator>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkSelectInProgress" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

However, the "Validate" script doesn't appear to run:

void Validate (object source,
System.Web.UI.WebControls.ServerValidateEventArgs e) {
e.IsValid = false;

DataGrid GridHolder;
foreach (DataListItem NewListItem in InProgressList.Items) {
GridHolder = NewListItem.FindControl("InProgressGrid") as DataGrid;
foreach (DataGridItem NewGridItem in GridHolder.Items) {
CheckBox chkSelectInProgress =
(CheckBox)NewGridItem.Cells[6].Controls[1];
TextBox CNumber = (TextBox)NewGridItem.Cells[5].Controls[1];

if (chkSelectInProgress.Checked == true) {
String Test = e.Value;
if (Test.Length > 0) {
e.IsValid = true;
}
}
}
}
}

Any suggestions?
Thanks,
Stephan.
 
P

Peter Blum

I can offer you a commercial solution that includes full client-side
validation support. My product, "Professional Validation And More", is a
replacement to Microsoft's validators that overcomes its many limitations.
What you've asked for is one of the most important limitations: the ability
to have a validator disable itself when some other condition (like a
checkbox) isn't right. All the validators in Professional Validation And
More include the Enabler property, where you define the condition.

Here's what the ASP.NET text of my RequiredTextValidator with the Enabler on
the CheckBox would look like:
<asp:checkbox id=checkbox1 runat=server>CheckMe</asp:checkbox>
<asp:textbox id=textbox1 runat=server />
<vam:RequiredTextValidator id=RTV1 runat=server ErrorMessage="Required"
ControlIDToEvaluate="TextBox1">
<EnablerContainer>
<vam:CheckStateCondition ControlIDToEvaluate="checkbox1" Checked=true
/>
</EnablerContainer>
</vam:RequiredTextValidator>

Aside from the Enabler, you get client-side validation on many more
browsers, 17 validators, and Validation Groups. Learn more at
http://www.peterblum.com/vam/home.aspx.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)

Stephan Bour said:
Hi,
I need to validate a text box in a datagrid nested inside a datalist. All I
need is to validate that any text is entered in the textbox. However, a
requiredfieldvalidator would not do because I want to perform the validation
only on the datagrid rows where a check box is checked.

Part of the datagrid:

<asp:TemplateColumn HeaderText="C #">
<ItemTemplate>
<asp:TextBox Width="50" MaxLenght="5" TextMode="SingleLine" ID="Cnumber"
runat="server" />
<asp:CustomValidator id="CNumNotNull"
ControlToValidate="Cnumber"
Display="Static"
InitialValue=""
OnServerValidate = "Validate"
ErrorMessage="Please enter a C Number"
runat="server">
*
</asp:CustomValidator>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Select" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="chkSelectInProgress" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>

However, the "Validate" script doesn't appear to run:

void Validate (object source,
System.Web.UI.WebControls.ServerValidateEventArgs e) {
e.IsValid = false;

DataGrid GridHolder;
foreach (DataListItem NewListItem in InProgressList.Items) {
GridHolder = NewListItem.FindControl("InProgressGrid") as DataGrid;
foreach (DataGridItem NewGridItem in GridHolder.Items) {
CheckBox chkSelectInProgress =
(CheckBox)NewGridItem.Cells[6].Controls[1];
TextBox CNumber = (TextBox)NewGridItem.Cells[5].Controls[1];

if (chkSelectInProgress.Checked == true) {
String Test = e.Value;
if (Test.Length > 0) {
e.IsValid = true;
}
}
}
}
}

Any suggestions?
Thanks,
Stephan.
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top