Hi,

I have a DropDownList (filled in the rowCreated method) in a TemplateColumn in a GridViewRow.
A user can add a row to select another value in the new dropdownlist created.

I need to trigger a validator (I need this because this is only a small part of a site that shows validations using the validatorcalloutextender and I am trying to avoid to simulate the funcionality of the validator) that validates if someone tries to select the same value of the dropdownlist...

I just wonder if that can be possible, or, I just have to settle with the simulation...

Thanks!

Dani AI

Generated

As discovered, checking for duplicate DropDownList selections across GridView rows is workable. Below are practical alternatives and tips that keep the ValidatorCalloutExtender behavior and avoid unnecessary full postbacks.

A lightweight client-side approach: add a CSS class to every dropdown in the grid (for example gridChoice) and give each dropdown a CustomValidator whose ClientValidationFunction scans all selects with that class and sets args.IsValid. This lets the ValidatorCalloutExtender show immediately when a duplicate is chosen.

function validateUniqueDDL(sender, args) {
  var ctl = document.getElementById(sender.controltovalidate);
  if (!ctl) { args.IsValid = true; return; }
  var value = ctl.value;
  if (!value) { args.IsValid = true; return; }
  var all = document.querySelectorAll('select.gridChoice');
  for (var i = 0; i < all.length; i++) {
    if (all[i] === ctl) continue;
    if (all[i].value === value) { args.IsValid = false; return; }
  }
  args.IsValid = true;
}

Server-side wiring notes (RowDataBound/RowCreated): set a unique ID on each CustomValidator and set its ClientValidationFunction (or ControlToValidate) and add the ValidatorCalloutExtender targeting that validator. You can also add an onchange attribute to each DropDownList to run just that validator without validating the whole page:

ddl.Attributes["onchange"] = "ValidatorValidate(document.getElementById('" + cv.ClientID + "'));";

Cautions and best practices: recreate dynamic controls and validators early (Page_Init/RowCreated) so validation works on postback; use a per-control validator if you want the callout next to the offending dropdown (a single global validator can validate the grid but cannot produce per-control callouts); prefer client-side validation for responsiveness and still validate server-side on submit to be safe.

Hi again,

I found my own answer... I used CustomValidator and set the OnServerValidate property to a method on the codebehind to go through the gridview comparing the SelectedValue of the current dropdownlist...

I hope that helps to someone... If anyone want some code, send me a private message...

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.