Custom control validation

Discussion in 'ASP .Net' started by tsw_mik, Dec 27, 2006.

  1. tsw_mik

    tsw_mik Guest

    I have created a custom control. It has:
    -label
    -button
    -list of textboxes
    -list of dropdownlists.
    I want to use a custom validator to perform some validation, but
    somehow I can't fo it. The code looks like this:

    public class ColumnSelector : Panel
    {
    public TextBox[] textBoxes;
    public DropDownList[] lists;
    CustomValidator validator;
    Label label;
    Button button;

    public ColumnSelector(int numberOfColumns, string[] values)
    {
    //initializing textboxes etc.
    validator = new CustomValidator();
    validator.ErrorMessage = "You must choose a different value for
    each column";
    lists[0].ID = "VALIDATED_CONTROL";
    validator.ControlToValidate = lists[0].ID;
    validator.Display = ValidatorDisplay.Dynamic;//?

    this.Controls.Add(label);
    for (int i = 0; i < number; i++)
    this.Controls.Add(textBoxes);
    for (int i = 0; i < number; i++)
    this.Controls.Add(lists);
    this.Controls.Add(button);
    this.Controls.Add(validator);
    }

    protected void validator_ServerValidate(object source,
    ServerValidateEventArgs args)
    {
    //validation here
    //if ok args.IsValid = true;
    //else args.IsValid = false;
    }

    protected void button_Click(object sender, EventArgs e)
    {
    validator.Validate();
    }
    }

    This control was created without using GUI so maybe I have forgotten
    something? (linking validator to the textbox?). Please let me know
    what's wrong with it.

    There is also some other problem, that looks like a bug to me, but
    maybe it isn't...
    I want all of the dropdownlists to have different selectedindex value
    initially (unless there's more dropdownlists than values), but somehow
    they all have the same value (the value of the last dropdownlist
    selected index actually).

    ListItem[] items = new ListItem[listOfItems];
    for (int i = 0; i < values.Length; i++)
    {
    items = new ListItem(values);
    }

    for (int i = 0; i < number; i++)
    {
    lists = new DropDownList();
    lists.Items.AddRange(items);
    if (i < values.Length) lists.SelectedIndex = i;
    else lists.SelectedIndex = values.Length - 1;
    }

    Is it a bug or my error somewhere?

    Mik
    tsw_mik, Dec 27, 2006
    #1
    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. Colin Mackay
    Replies:
    0
    Views:
    2,677
    Colin Mackay
    Jun 25, 2003
  2. =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=

    Using Summary Validation control with server Custom validation

    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=, Oct 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    869
    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=
    Oct 15, 2004
  3. Jens Jensen
    Replies:
    0
    Views:
    425
    Jens Jensen
    Jul 7, 2006
  4. JimmyLoh
    Replies:
    4
    Views:
    1,551
    deaconb
    Aug 19, 2008
  5. Matt Sokol
    Replies:
    2
    Views:
    365
    Theo Tillotson
    Aug 7, 2003
Loading...

Share This Page