mk2 mk2 - 1 year ago 108
ASP.NET (C#) Question

Dynamic Jquery Validation of Dropdownlist

I'm attempting to dynamically add jquery validation to some dynamically created items in my webforms project (asp:checkbox, asp:textbox, infragistics:webnumericeditor, asp:dropdownlist) and can get it working correctly for all items except the dropdownlist.

The code I'm using is:

var oID = this.ObjectID;
$("#" + oID).rules("add", {
required: {
depends: function (element) {
return $("#" + oID + " option:selected").val() == 0;
messages: {
required: "*"

The dropdownlist item is generated from codebehind with a 'Please Select...' item inserted at position 0 like so

DropDownList ddl = new DropDownList();
ListItem li = new ListItem("Please Select...", "0");
ddl.Items.Insert(0, li);

The validation is never firing though. I added an alert to fire when the item gets generated and that returns 0 correctly so I'm unsure why the validation isn't working.

This is my first time using jquery validation so I'm expecting it to be something obvious and simple but the methods I've tried thus far haven't yielded any results so any help is much appreciated!

Answer Source
ListItem li = new ListItem("Please Select...", " ");

Assign ' ' for your default item instead of assigning to a value of '0'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download