radgrid duplicate record validation

2 posts, 0 answers
  1. Farrukh
    Farrukh avatar
    12 posts
    Member since:
    May 2009

    Posted 04 Jan Link to this post

    Hi,

     I am using radgrid version 2015 q2 template column and edit template column I want to implement duplicate record validation in insert and edit mode but I am not able to find controls in insert and edit mode of radgrid  below is my code.

    If there is another easy method to validate dublicate record in radgrid plz share with me  thanks.

     

    <telerik:RadGrid ID="grdLanguage" runat="server" AllowSorting="True" AllowPaging="True"
                                AllowCustomPaging="True" PagerStyle-Mode="NextPrev" Skin="Bootstrap"
                                ShowGroupPanel="false">
                                <GroupingSettings CaseSensitive="false" />
     
                                <MasterTableView AutoGenerateColumns="False" EditMode="PopUp" DataKeyNames="Language_ID" CommandItemDisplay="Top">
                                    <EditFormSettings>
                                        <EditColumn FilterControlAltText="Filter EditCommandColumn1 column" UniqueName="EditCommandColumn1">
                                        </EditColumn>
                                        <PopUpSettings Modal="true" />
                                    </EditFormSettings>
                                    <CommandItemTemplate>
                                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsert" Visible='<%# !grdLanguage.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/AddRecord.png"/>Add new</asp:LinkButton>  
                                    </CommandItemTemplate>
                                    <RowIndicatorColumn>
                                        <HeaderStyle Width="20px"></HeaderStyle>
                                    </RowIndicatorColumn>
                                    <ExpandCollapseColumn>
                                        <HeaderStyle Width="20px"></HeaderStyle>
                                    </ExpandCollapseColumn>
                                    <Columns>
                                        <telerik:GridTemplateColumn HeaderText="Language Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblCertName" runat="server" Text='<%# Bind("Language_Type.Language_Type_Name") %>'></asp:Label>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn HeaderText="Reading">
                                            <ItemTemplate>
                                                <telerik:RadRating ID="ratReading" runat="server" DbValue='<%# Bind("Read_Skill") %>' ItemCount="5" ReadOnly="true"></telerik:RadRating>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridTemplateColumn HeaderText="Writing">
                                            <ItemTemplate>
                                                <telerik:RadRating ID="ratWriting" runat="server" DbValue='<%# Bind("Writing_Skill") %>' ItemCount="5" ReadOnly="true"></telerik:RadRating>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
     
                                        <telerik:GridTemplateColumn HeaderText="Speaking">
                                            <ItemTemplate>
                                                <telerik:RadRating ID="ratSpeaking" runat="server" DbValue='<%# Bind("Speaking_Skill") %>' ItemCount="5" ReadOnly="true"></telerik:RadRating>
                                            </ItemTemplate>
                                        </telerik:GridTemplateColumn>
     
                                        <telerik:GridEditCommandColumn HeaderText="Edit" ButtonType="ImageButton"></telerik:GridEditCommandColumn>
                                    </Columns>
                                    <EditFormSettings EditFormType="Template" PopUpSettings-Height="350px" PopUpSettings-Width="600px">
                                        <FormTemplate>
     
                                            <div class="form-horizontal panel panel-primary">
                                                <div class="panel-heading">Previous Experience</div>
                                                <div class="panel-body">
                                                    <div>
                                                        <asp:CustomValidator ID="CustValLanguage" OnServerValidate="CustValLanguage_ServerValidate1" ValidationGroup="valLanguage" runat="server" Display="Dynamic"> </asp:CustomValidator>
                                                    </div>
                                                    <div class="form-group required">
                                                        <asp:Label ID="Label1" CssClass="col-sm-2 control-label" runat="server" Text="Degree"></asp:Label>
     
                                                        <div class="col-sm-10">
                                                            <telerik:RadComboBox ID="ddlLanguage_Type_ID" SelectedValue='<%# Bind("Language_Type_ID") %>' runat="server" Skin="Bootstrap">
                                                                <Items>
                                                                    <telerik:RadComboBoxItem Text="[Select Item]" Value="0" runat="server" />
                                                                    <telerik:RadComboBoxItem Text="Arabic" Value="1" runat="server" />
                                                                    <telerik:RadComboBoxItem Text="English" Value="2" runat="server" />
                                                                    <telerik:RadComboBoxItem Text="Farsi" Value="5" runat="server" />
                                                                    <telerik:RadComboBoxItem Text="French" Value="3" runat="server" />
                                                                    <telerik:RadComboBoxItem Text="German" Value="4" runat="server" />
                                                                </Items>
                                                            </telerik:RadComboBox>
                                                            <br />
                                                            <span>
                                                                <asp:RequiredFieldValidator ControlToValidate="ddlLanguage_Type_ID" InitialValue="[Select Item]" Display="Dynamic" SetFocusOnError="true" CssClass="text-danger" runat="server" ID="RequiredFieldValidator5" ErrorMessage="Language is Required!"
                                                                    ValidationGroup="valLanguage"></asp:RequiredFieldValidator></span>
                                                        </div>
                                                    </div>
     
                                                    <div class="form-group ">
                                                        <asp:Label ID="lblCertName" CssClass="col-sm-2 control-label" runat="server" Text="Reading"></asp:Label>
     
                                                        <div class="col-sm-10">
                                                            <telerik:RadRating ID="ratReading" runat="server" Precision="Item" DbValue='<%# Bind("Read_Skill") %>' ItemCount="5"></telerik:RadRating>
                                                            <span></span>
                                                        </div>
                                                    </div>
                                                    <div class="form-group ">
                                                        <asp:Label ID="lblCertAuthority" CssClass="col-sm-2 control-label" runat="server" Text="Writing"></asp:Label>
     
                                                        <div class="col-sm-10">
                                                            <telerik:RadRating ID="ratWriting" runat="server" Precision="Item" DbValue='<%# Bind("Writing_Skill") %>' ItemCount="5"></telerik:RadRating>
     
                                                            <span></span>
                                                        </div>
                                                    </div>
                                                    <div class="form-group ">
                                                        <asp:Label ID="Label2" CssClass="col-sm-2 control-label" runat="server" Text="Speacking"></asp:Label>
     
                                                        <div class="col-sm-10">
                                                            <telerik:RadRating ID="ratSpeaking" Precision="Item" runat="server" DbValue='<%# Bind("Speaking_Skill") %>' ItemCount="5"></telerik:RadRating>
                                                            <span></span>
                                                        </div>
                                                    </div>
     
                                                    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                        runat="server" ValidationGroup="valLanguage" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button
                                                    <asp:Button ID="btnCancel" formnovalidate name="cancel" class="cancel" Text="Cancel" runat="server" CausesValidation="False"
                                                        CommandName="Cancel"></asp:Button>
                                                </div>
                                            </div>
     
                                        </FormTemplate>
                                    </EditFormSettings>
                                </MasterTableView>
                                <ClientSettings>
                                    <ClientEvents OnPopUpShowing="PopUpShowing" />
                                </ClientSettings>
                                <PagerStyle Mode="NextPrev" />
                            </telerik:RadGrid>

     

     

     ServerSide Code

     

      protected void CustValLanguage_ServerValidate1(object source, ServerValidateEventArgs args)
            {
               // Need here to validate code
     
     if (lstLanguage != null || lstLanguage.Count > 0)
                {             
                    if (grdLanguage.MasterTableView.IsItemInserted)
                    {
                        {
                            RadComboBox ddlLanguage_Type_ID = (RadComboBox)grdLanguage.MasterTableView.GetInsertItem().FindControl("ddlLanguage_Type_ID");
                            for (int i = 0; i < lstLanguage.Count; i++)
                            {
                                if (lstLanguage[i].Language_Type_ID == Convert.ToInt32(ddlLanguage_Type_ID.SelectedValue))
                                {
                                    args.IsValid = false;
                                    CustomValidator CustValLanguage = (CustomValidator)grdLanguage.MasterTableView.GetInsertItem().FindControl("CustValLanguage");
                                    CustValLanguage.ErrorMessage = "Already Exists";
                                }
                            }
     
     
                        }
                    }
                    else
                    {
                       
    //                 I want code here to check update record already exist or not in the current list collection
                    }
                }
            }

     

     

    private List<Language> lstLanguage
            {
                get
                {
                    object obj = this.Session["lstLanguage"];
                    if ((!(obj == null)))
                    {
                        return ((List<Language>)(obj));
                    }
                    List<Language> lst_Lang = new List<Language>();
                    this.Session["lstLanguage"] = lst_Lang;
                    return lst_Lang;
                }
                set
                {
                    // Adds value to session variable
                    this.Session["lstLanguage"] = value;
                }
            }

    private void GrdLanguage_ItemCommand(object sender, GridCommandEventArgs e)
           {
     
               switch (e.CommandName)
               {
                   case "PerformInsert":
                       if (Page.IsValid)
                       {
                           GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();
     
                           Language obj = new Language();
                           obj.Language_ID = (int)this.lstLanguage.Count + 1;
                           obj.Language_Type_ID = Convert.ToInt32(((RadComboBox)insertedItem.FindControl("ddlLanguage_Type_ID")).SelectedValue);
                           obj.Language_Type.Language_Type_Name = ((RadComboBox)insertedItem.FindControl("ddlLanguage_Type_ID")).SelectedItem.Text;
                           obj.Read_Skill = Convert.ToInt32(((RadRating)insertedItem.FindControl("ratReading")).Value);
                           obj.Writing_Skill = Convert.ToInt32(((RadRating)insertedItem.FindControl("ratWriting")).Value);
                           obj.Speaking_Skill = Convert.ToInt32(((RadRating)insertedItem.FindControl("ratSpeaking")).Value);
     
                           lstLanguage.Add(obj);
                           grdLanguage.Rebind();
     
                       }
                       break;
                   case "Update":
                       if (Page.IsValid)
                       {
                           //  GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();
                           GridEditableItem editform = e.Item as GridEditableItem;
     
                           var index = lstLanguage.FindIndex(p => p.Language_ID == (int)editform.OwnerTableView.DataKeyValues[editform.ItemIndex]["Language_ID"]);
     
                           Language obj = new Language();
                           obj.Language_Type_ID = Convert.ToInt32(((RadComboBox)editform.FindControl("ddlLanguage_Type_ID")).SelectedValue);
                           obj.Language_Type.Language_Type_Name = ((RadComboBox)editform.FindControl("ddlLanguage_Type_ID")).SelectedItem.Text;
                           obj.Read_Skill = Convert.ToInt32(((RadRating)editform.FindControl("ratReading")).Value);
                           obj.Writing_Skill = Convert.ToInt32(((RadRating)editform.FindControl("ratWriting")).Value);
                           obj.Speaking_Skill = Convert.ToInt32(((RadRating)editform.FindControl("ratSpeaking")).Value);
     
     
                           lstLanguage[index] = obj;
                           grdLanguage.Rebind();
     
                       }
                       break;
                   default:
                       break;
               }
           }
     
           private void GrdLanguage_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
           {
               grdLanguage.DataSource = lstLanguage;
           }

     

     

  2. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 07 Jan Link to this post

    Hi Farrukh,

    In order to access the controls in edit form you can get the it from the EditItem collection.
    GridEditableItem editItem = RadGrid1.EditItems[0] as GridEditableItem;

    After you get the edit item you can use the approach from the following help article to access the controls in it. Keep in mind that you need to make a request to the server and compare whether the updated value already exists in the database or not and make the changes.

    Regards,
    Kostadin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top