This is a migrated thread and some comments may be shown as answers.

ClientSide Custom Validator on Edit/InsertTempalte

3 Answers 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prava kafle
Top achievements
Rank 1
Prava kafle asked on 07 Feb 2013, 10:00 PM
Hi ,

I am using  edit  and insert template to display  grid items in edit mode. I have  several  comboboxes in edittemplate and would like to have customvalidator that runs on client side.

If tselected index of the combobox  is "0" then it should not let user insert/edit a record. Code  shown below doesnot work at all times, especially when I have multiple comboboxes.


Javascript fxn
 function ValidateComboBoxInNGGridEditForm(customValidator, arguments) {
                         if (arguments.Value == "< Select a Value >") {
                             arguments.IsValid = false; 
                         } else {
                             arguments.IsValid = true; 
                         }
                    }


c# Code

   case "REQCOMBOBOX":
                        RadComboBox comboBox = new RadComboBox();
                        comboBox.Width = new Unit(215.0, UnitType.Pixel);
                     .....................
                       
                        comboBox.SelectedIndex = -1;
                            CustomValidator customValidator = new CustomValidator();
                            customValidator.ID = comboBox.ID + "REQFVLD";
                            customValidator.ControlToValidate = comboBox.ID;
                            customValidator.ClientValidationFunction = "ValidateComboBoxInNGGridEditForm" ;
                            customValidator.ErrorMessage = " *";


Am I missing anything here?

Thanks,
Prava

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Feb 2013, 11:21 AM
Hi Prava,

I guess you are creating the entire grid from code behind. Please have a look into the following sample code which works fine at my end.

ASPX:
<div>
       <asp:ScriptManager ID="ScriptManager1" runat="server">
       </asp:ScriptManager>
       <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
   </div>
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
       SelectCommand="SELECT top 5  EmployeeID,FirstName,LastName FROM [Employees] ">
   </asp:SqlDataSource>

C#:
protected void Page_Init(object sender, EventArgs e)
   {
       RadGrid grid = new RadGrid();
       grid.AutoGenerateColumns = false;
       grid.AutoGenerateEditColumn = true;
       grid.DataSourceID = "SqlDataSource1";
    
       GridBoundColumn boundColumn = new GridBoundColumn();
       boundColumn.DataField = "EmployeeID";
       boundColumn.UniqueName = "EmployeeID";
       boundColumn.HeaderText = "EmployeeID";
       grid.MasterTableView.Columns.Add(boundColumn);
 
       string templateColumnName = "FirstName";
       GridTemplateColumn templateColumn = new GridTemplateColumn();
       templateColumn.ItemTemplate = new MyItemTemplate(templateColumnName);
       templateColumn.EditItemTemplate = new MyEditItemTemplate(templateColumnName);
       templateColumn.DataField = templateColumnName;
       templateColumn.HeaderText = templateColumnName;
       templateColumn.UniqueName = templateColumnName;
       grid.MasterTableView.Columns.Add(templateColumn);
 
       templateColumnName = "LastName";
       templateColumn = new GridTemplateColumn();
       templateColumn.ItemTemplate = new MyItemTemplate(templateColumnName);
       templateColumn.EditItemTemplate = new MyEditItemTemplate(templateColumnName);
       templateColumn.DataField = templateColumnName;
       templateColumn.HeaderText = templateColumnName;
       templateColumn.UniqueName = templateColumnName;
       grid.MasterTableView.Columns.Add(templateColumn);
 
       PlaceHolder1.Controls.Add(grid);
   }
   public class MyItemTemplate : ITemplate
   {
       Label lbl;
       private string colname;
       public MyItemTemplate(string cName)
       {
           colname = cName;
       }
       public void InstantiateIn(Control container)
       {
           lbl = new Label();
           lbl.ID = colname + "_Label";
           lbl.DataBinding += new EventHandler(lbl_DataBinding);
           container.Controls.Add(lbl);
       }
 
       void lbl_DataBinding(object sender, EventArgs e)
       {
           Label lbl = sender as Label;
           GridDataItem container = (GridDataItem)lbl.NamingContainer;
           lbl.Text = ((DataRowView)container.DataItem)[colname].ToString();
       }
   }
   public class MyEditItemTemplate : IBindableTemplate
   {
       RadComboBox combo;
       CustomValidator customValidator;
       private string colname;
       public MyEditItemTemplate(string cName)
       {
           colname = cName;
       }
       public IOrderedDictionary ExtractValues(Control container)
       {
           OrderedDictionary dictionary = new OrderedDictionary();
           dictionary.Add(colname, (container.FindControl(colname + "_RadComboBox") as RadComboBox).Text);
           return dictionary;
       }
 
       public void InstantiateIn(Control container)
       {
           combo = new RadComboBox();
           combo.ID = colname + "_RadComboBox";
           combo.DataBinding += new EventHandler(combo_DataBinding);
 
           customValidator = new CustomValidator();
           customValidator.ID = combo.ID + "REQFVLD";
           customValidator.ControlToValidate = combo.ID;
           customValidator.ClientValidationFunction = "ValidateComboBoxInNGGridEditForm";
           customValidator.ErrorMessage = " *";
 
           container.Controls.Add(combo);
           container.Controls.Add(customValidator);
       }
       void combo_DataBinding(object sender, EventArgs e)
       {
           RadComboBox txt = sender as RadComboBox;
           txt.AppendDataBoundItems = true;
           txt.Items.Insert(0, new RadComboBoxItem("Select", string.Empty));
           if (colname == "FirstName")
           {
           txt.DataSourceID = "SqlDataSource1";
           txt.DataTextField = "FirstName";
           txt.DataValueField = "FirstName";
           }
           if (colname == "LastName")
           {
               txt.DataSourceID = "SqlDataSource1";
               txt.DataTextField = "LastName";
               txt.DataValueField = "LastName";
           }
       }
   }

Javascript:
<script type="text/javascript">
    function ValidateComboBoxInNGGridEditForm(sender, arguments) {
        if (arguments.Value == "Select") {
            arguments.IsValid = false;
        } else {
            arguments.IsValid = true;
        }
    
</script>

Thanks,
Princy.
0
Prava kafle
Top achievements
Rank 1
answered on 08 Feb 2013, 01:36 PM
Hi Princy,

Thanks for taking time to create a sample code for me. Yes, I am creating a dynamic grid on buttonclick event and attaching it in placeholder .
I have one customtext combobox and a regular combobox in edit template. I found two issues  while using custom validator.

1. when you make a change in comboboxes and set "Select an item", it does show "*"  after you change the focus  from that control.
   Click update/insert, you see Red "*" next to comboxes and does not allow you to insert.

    But if you make some changes -- repeat above steps,this time it does not validate.

2. Make change -see validation error on both controls .  --> Change  selected index  > 0 , click "save".
     It throws an error/ it rebinds the grid and you loose  expanded state of gridhierarchy





Is it possible to do group validation and alert a single message on Insert/Edit template?
Regards,
Prava
     
0
Princy
Top achievements
Rank 2
answered on 11 Feb 2013, 06:45 AM
Hi Prava,

I have set 'AllowCustomText' property to one ComboBox and still couldn't replicate the issue.  Can you please paste your complete code for further assistance?

Thanks,
Princy.
Tags
Grid
Asked by
Prava kafle
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Prava kafle
Top achievements
Rank 1
Share this question
or