Custom validator inside DataGrid

Discussion in 'ASP .Net' started by Stephan Bour, Jan 16, 2004.

  1. Stephan Bour

    Stephan Bour Guest

    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.
     
    Stephan Bour, Jan 16, 2004
    #1
    1. Advertising

  2. Stephan Bour

    Peter Blum Guest

    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:

    "Stephan Bour" <> wrote in message
    news:BC2D800A.DDD8%...
    > 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.
    >
     
    Peter Blum, Jan 16, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    356
  2. Niclas

    Custom Validator inside datagrid

    Niclas, Jan 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    674
    Niclas
    Jan 3, 2006
  3. Replies:
    4
    Views:
    904
    Richter~9.6
    Feb 13, 2007
  4. Scott Yenor

    How to get custom control to work with custom validator

    Scott Yenor, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    213
    Scott Yenor
    Jan 27, 2004
  5. Radu
    Replies:
    0
    Views:
    287
Loading...

Share This Page