Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add CustomValidator Dynamically

Status
Not open for further replies.

Peppi

Programmer
Apr 9, 2001
205
CA
Hello,

I want to add CustomValidator controls dynamically to my user control. I'm having problems trying to specify what should happen for the ServerValidate property. What would this look like?

Thx.
 
Not sure what you mean. ServerValidate is an Event not a Property. You will need to use AddHandler if you are using VB to associate an event with a control when adding it dynamically.
 
Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        Enter A Number 
        <asp:TextBox 
            ID="myTextBox" 
            runat="server" 
            AutoPostBack="true" 
            CausesValidation="true" 
            OnTextChanged="myTextBox_TextChanged" />
        <asp:Label 
            ID="myLabel" 
            runat="server" />
    </form>
</body>
</html>
using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //NOTE: Do not check for postback.

        //declare new control
        CustomValidator myCustomValidator = new CustomValidator();
        //set properties
        myCustomValidator.ID = "myCustomValidator";
        myCustomValidator.ControlToValidate = myTextBox.ID;
        myCustomValidator.ErrorMessage = "NaN.";

        //define event
        myCustomValidator.ServerValidate += new ServerValidateEventHandler(myCustomValidator_ServerValidate);

        //add control to page... you probally want to add this next to the control to validate.
        Page.Form.Controls.Add(myCustomValidator);
    }

    void myCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int returnValue = 0;
        args.IsValid = int.TryParse(args.Value, out returnValue);
    }

    protected void myTextBox_TextChanged(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            myLabel.Text = "you entered: " + myTextBox.Text;
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK, here is what I have coded in the RowDataBound event, but my event handler is not being called to perform validation. My ValidationSummary control and the button that causes the validation are both in the same ValidationGroup.

Did I miss any steps?

Code:
Dim daysRequestedRangeValidator As New CustomValidator
Dim cell As New TableCell

daysRequestedRangeValidator.ControlToValidate="txtDays"    
daysRequestedRangeValidator.ErrorMessage = "Message here."
daysRequestedRangeValidator.Text = "&nbsp;"
daysRequestedRangeValidator.ValidationGroup = "ExistingRequest"
daysRequestedRangeValidator.Display = ValidatorDisplay.Dynamic
AddHandler daysRequestedRangeValidator.ServerValidate, AddressOf ValidateDaysRequestedRange

cell.Controls.Add(daysRequestedRangeValidator)
e.Row.Cells.Add(cell)

Sub ValidateDaysRequestedRange(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
'Handle event.
End Sub

 
For now, remove the validation group of the validator and see if it gets fired.
 
could you post more code, including the gridview syntax, and all relavent code behind (Init, Load, GridViewEvents, custom private methods on the page that effect this).

There could be a number of reasons why this isn't executing correctly. more information will help narrow down the problem.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I've changed it so that I am no longer adding the validators dynamically. I'm just adding a validator for each textbox and then in the validation event I'm checking certain criteria to see whether or not validation needs to be performed.

However, I have a new problem now. When the validation event fires, the value of args.Value is the old value, not the new value that the user has entered. Why would that be?
 
I think it's because the validation control is part of a user control that I'm loading dynamically, so args.value is not being preserved? Can I somehow save it in ViewState?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top