Custom control validation

T

tsw_mik

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
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top