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

[Solved] CustomValidation on Insert ONLY

2 Answers 141 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas Frohe
Top achievements
Rank 1
Thomas Frohe asked on 16 Dec 2009, 07:23 PM
Greetings,

    I have a grid in which I am doing some RequiredField validations. I also have a single CustomValidator that tests if the specific value already exists in my DB. I only want the custom validation to happen when I am inserting new records. How do I check in my ServerValidation method, which state the grid is in. i am still learning this stuff so please be patient with me.

Here is the the grid column that I am trying to validate.
<telerik:GridTemplateColumn DataField="EID" HeaderText="EID" UniqueName="EID"
    <ItemTemplate> 
        <asp:Label runat="server" ID="lblEID" Text='<%# Eval("EID", "{0}") %>'></asp:Label> 
    </ItemTemplate> 
    <EditItemTemplate> 
        <telerik:RadTextBox runat="server" ID="tbEID" Skin="Office2007" Width="158" 
            Text='<%# DataBinder.Eval(Container, "DataItem.EID") %>'
        </telerik:RadTextBox> 
        <span style="color: Red">*</span> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbEID" 
            ErrorMessage="EID is required" runat="server"></asp:RequiredFieldValidator> 
        <asp:CustomValidator ID="EIDValidator" ControlToValidate="tbEID" OnServerValidate="CheckEID" 
            ErrorMessage="This user already exists" runat="server"></asp:CustomValidator> 
    </EditItemTemplate> 
</telerik:GridTemplateColumn> 

and the Validation in the code-behind
protected void CheckEID(object source, ServerValidateEventArgs args) 
    DataSet ds = new DataSet(); 
    SqlParameter[] param = new SqlParameter[1]; 
 
    param[0] = ExpertProvider.SqlDB.MakeParameter("@EID", args.Value); 
    ExpertProvider.SqlDB.RunProcedure("Database.Expert.ConnectionString"
            "HSOExpertSystem_Admin_Users_Get_User", param, ref ds); 
 
    if (ds.Tables[0].Rows.Count > 0) 
    { 
        args.IsValid = false
    } 
    else 
    { 
        args.IsValid = true
    } 

I'd be grateful for any assistance. Thanks


2 Answers, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 19 Dec 2009, 07:25 AM
Hi Thomas,

You could check if your grid is in insert mode with RadGrid1.MasterTableView.IsItemInserted property. So you could modified your code like that:

protected void CheckEID(object source, ServerValidateEventArgs args)
   {
       if (RadGrid1.MasterTableView.IsItemInserted)
       {
           DataSet ds = new DataSet();
           SqlParameter[] param = new SqlParameter[1];
 
           param[0] = ExpertProvider.SqlDB.MakeParameter("@EID", args.Value);
           ExpertProvider.SqlDB.RunProcedure("Database.Expert.ConnectionString",
                   "HSOExpertSystem_Admin_Users_Get_User", param, ref ds);
 
           if (ds.Tables[0].Rows.Count > 0)
           {
               args.IsValid = false;
           }
           else
           {
               args.IsValid = true;
           }
       }
   }

The body of the “if” statement will be executed only when your grid is in insert mode and user click on insert button. I hope this helps.

All the best,
Radoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas Frohe
Top achievements
Rank 1
answered on 21 Dec 2009, 01:18 PM
Thank you for the reply. It works perfectly now.
Tags
Grid
Asked by
Thomas Frohe
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Thomas Frohe
Top achievements
Rank 1
Share this question
or