Listbox Validator

  • Thread starter Thread starter Nightcrawler
  • Start date Start date
N

Nightcrawler

I have two listboxes. The on to the left is populated with database
columns and the one the right is empty. There are two buttons in
between the listboxes where users can add and remove between the two
listboxes.

When the user is done adding columns to the listbox to the right and
clicks continue, I want to ensure that the right listbox contains two
required columns (article, title).

I could do this validation on the postback where I look for the values
but I was wondering if there is a way of doing it using the built in
validators on the client side instead?

Please let me know.

Thanks
 
Sure,

Just add a custom validator and then write both client-side (javascript)
function and OnServerValidate server side.
 
Do you know where I can find more information about this? You don't
happen to have a sample you can share?
 
You can always look up information on CustomValidator in MSDN

Unfortunately I did not have the same requirement in the past, but you code
will need to look something similar to the following:

Page_Load:
if (!IsPostBack){
this.ViewState["columns"] = "article,title"; //getYouRequiredColumnsList
}

Page_PreRender:
string cols = this.ViewState["columns"].ToString();
string script = @"function list_req_cols_validate(src, e){
var opts = document.getElementById('" + ddlSelectedColumns.ClientID +
@"').options;
e.IsValid = opts.length > 2;
if (e.IsValid){
var cols = '," + cols + @",';
var count = 0;
for (var i=0; i<opts.length; i++){
if (cols.indexOf(opts.text) != -1)
count++;
}
e.IsValid = (count==" + cols.Split(',').Length + @");
}
}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myValidatorScript",
script, true);


and similar code in the ServerValidate - I have not tested the code, but
this should give you a starting point
 
You can always look up information on CustomValidator in MSDN

Unfortunately I did not have the same requirement in the past, but you code
will need to look something similar to the following:

Page_Load:
if (!IsPostBack){
this.ViewState["columns"] = "article,title"; //getYouRequiredColumnsList

}

Page_PreRender:
string cols = this.ViewState["columns"].ToString();
string script = @"function list_req_cols_validate(src, e){
var opts = document.getElementById('" + ddlSelectedColumns.ClientID +
@"').options;
e.IsValid = opts.length > 2;
if (e.IsValid){
var cols = '," + cols + @",';
var count = 0;
for (var i=0; i<opts.length; i++){
if (cols.indexOf(opts.text) != -1)
count++;
}
e.IsValid = (count==" + cols.Split(',').Length + @");
}}";

ClientScript.RegisterClientScriptBlock(this.GetType(), "myValidatorScript",
script, true);

and similar code in the ServerValidate - I have not tested the code, but
this should give you a starting point



Nightcrawler said:
Do you know where I can find more information about this? You don't
happen to have a sample you can share?- Hide quoted text -

- Show quoted text -


Looks like it's working. However, I noticed that the event that
triggers the validation is the moment at least 1 item in the
destination listbox is selected. Until at least one item is selcted
the validation won't happen. Ie, if I add article and something esle,
I have to click on an item to get the validation to work. If I then
add title, the validation won't go away until I click a different item
in the listbox to execute the validation again.

Not sure how to figure this out but I will play around with it.
 
you need to trigger the validation yourself from the javascript that you call
in your move buttons - something similar to the following:

if (typeof(ValidatorEnable) != 'undefined') ValidatorEnable(val, true);
// val - would be defined prior as a reference to your validator span - note
that document.getElementById('" + myVal.ClientID + "'); will not work here

ValidatorEnable is a built in ASP.Net validator function that, if the second
parameter (enable) is true, revalidates the validator. The default behavior
would be to wait until the target control onchange event occurs - and this
only happens when you change selection in the control (as you described).

If you not sure how to get a reference of your validator - you can get an
IDable control (such as INPUT, SELECT, DIV, etc) on the same level and then
work your way down the DOM heirarchy (say using nextSibling property) until
you get an element with tagName=='SPAN' and controltovalidate attribute ==
your listbox' ClientID.

Let me know if you have any more questions.



Nightcrawler said:
You can always look up information on CustomValidator in MSDN

Unfortunately I did not have the same requirement in the past, but you code
will need to look something similar to the following:

Page_Load:
if (!IsPostBack){
this.ViewState["columns"] = "article,title"; //getYouRequiredColumnsList

}

Page_PreRender:
string cols = this.ViewState["columns"].ToString();
string script = @"function list_req_cols_validate(src, e){
var opts = document.getElementById('" + ddlSelectedColumns.ClientID +
@"').options;
e.IsValid = opts.length > 2;
if (e.IsValid){
var cols = '," + cols + @",';
var count = 0;
for (var i=0; i<opts.length; i++){
if (cols.indexOf(opts.text) != -1)
count++;
}
e.IsValid = (count==" + cols.Split(',').Length + @");
}}";

ClientScript.RegisterClientScriptBlock(this.GetType(), "myValidatorScript",
script, true);

and similar code in the ServerValidate - I have not tested the code, but
this should give you a starting point



Nightcrawler said:
Do you know where I can find more information about this? You don't
happen to have a sample you can share?- Hide quoted text -

- Show quoted text -


Looks like it's working. However, I noticed that the event that
triggers the validation is the moment at least 1 item in the
destination listbox is selected. Until at least one item is selcted
the validation won't happen. Ie, if I add article and something esle,
I have to click on an item to get the validation to work. If I then
add title, the validation won't go away until I click a different item
in the listbox to execute the validation again.

Not sure how to figure this out but I will play around with it.
 
Back
Top