title: FindControl inside RadGrid (Gridview) Edit and Insert views
body: <p>I have a RadGrid (Gridview) that has the option to edit and insert new items.</p>
  
  <p>Within both those options i have a customvalidator to check for existing values so duplicates cannot be entered. What i am stuck on is the ability to use findcontrol for the textbox that only is shown during EDIT and INSERT.</p>
  
  <p>This works great for INSERT as i call GetInsertItem(), but that throws an error when editing and i am stuck on that. I want to do this outside of the databound and command methods. </p>
  
  <pre><code>protected void cvAccountNumber_ServerValidate(object source, ServerValidateEventArgs args)
  {
      TextBox txtAccountNumber = (TextBox)RadGrid1.MasterTableView.GetInsertItem().FindControl("txtAccountNumber");
  
      List&lt;GridDataItem&gt; Items = (from item in RadGrid1.Items.Cast&lt;GridDataItem&gt;()
                                  where item["AccountNumberView"].Text == txtAccountNumber.Text
                                  select item).ToList();
  
      args.IsValid = Items.Count() == 0;
  }
  </code></pre>
  
  <p>Here is an image of the edit screen.
  <img src="https://i.stack.imgur.com/ilpeU.jpg" alt="enter image description here"></p>
  
  <p>ASPX:</p>
  
  <pre><code>Sort By Status: &lt;asp:DropDownList ID="dd_Status" runat="server" Width="150px" 
      onselectedindexchanged="dd_Status_SelectedIndexChanged" 
      AutoPostBack="True"&gt;
      &lt;asp:ListItem Text="All" Value="0" Selected="True"&gt;&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Text="Active" Value="True"&gt;&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Text="Inactive" Value="False"&gt;&lt;/asp:ListItem&gt;
  &lt;/asp:DropDownList&gt;
  
  &lt;br /&gt;&lt;br /&gt;
  
  &lt;telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" Width="500px"
      GridLines="None" AllowFilteringByColumn="False" AllowSorting="True" OnExportCellFormatting="RadGrid1_ExportCellFormatting" DataSourceID="SqlDataSource1"
       AllowAutomaticDeletes="true" AllowAutomaticUpdates="True" AutoGenerateEditColumn="True"  AutoGenerateDeleteColumn="true" OnItemCommand="RadGrid1_ItemCommand" &gt;
      &lt;MasterTableView AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" ItemStyle-HorizontalAlign="Left" CommandItemDisplay="TopAndBottom"&gt;
          &lt;Columns&gt;
              &lt;telerik:GridTemplateColumn HeaderText="ID" SortExpression="ID" UniqueName="ID" Visible="false" ReadOnly="true"&gt; 
                  &lt;ItemTemplate&gt; 
                      &lt;asp:Label ID="lblIDView" runat="server" Text='&lt;%# Bind("ID") %&gt;'&gt;&lt;/asp:Label&gt; 
                  &lt;/ItemTemplate&gt; 
              &lt;/telerik:GridTemplateColumn&gt;
              &lt;telerik:GridTemplateColumn HeaderText="ID" SortExpression="IDEdit" UniqueName="IDEdit" Visible="false"&gt;
                  &lt;ItemTemplate /&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:Label ID="lblID" runat="server" Text='&lt;%# Bind("ID") %&gt;'&gt;&lt;/asp:Label&gt; 
                  &lt;/EditItemTemplate&gt;
              &lt;/telerik:GridTemplateColumn&gt;
              &lt;telerik:GridBoundColumn DataField="AccountNumber" HeaderText="Sapphire Account Number" 
                  SortExpression="AccountNumber" UniqueName="AccountNumberView" ReadOnly="true" ItemStyle-Width="400"&gt;
              &lt;/telerik:GridBoundColumn&gt;
              &lt;telerik:GridTemplateColumn HeaderText="Sapphire Account Number" SortExpression="AccountNumber" UniqueName="AccountNumber" Visible="false"&gt;
                  &lt;ItemTemplate /&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:Textbox ID="txtAccountNumber" runat="server" Text='&lt;%# Bind("AccountNumber") %&gt;' /&gt;
                      &lt;asp:RequiredFieldValidator ID="rfvAccountNumber" ControlToValidate="txtAccountNumber"
                          ErrorMessage="Sapphire Account Number is required" runat="server" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;
                      &lt;asp:CustomValidator ID="cvAccountNumber" runat="server" ErrorMessage="Sapphire Account Number already exists"
                          ControlToValidate="txtAccountNumber" ForeColor="Red" OnServerValidate="cvAccountNumber_ServerValidate"&gt;&lt;/asp:CustomValidator&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/telerik:GridTemplateColumn&gt;
              &lt;telerik:GridTemplateColumn HeaderText="Active" SortExpression="Active" UniqueName="Active" ItemStyle-Width="100" Visible="false"&gt;
                  &lt;ItemTemplate /&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:CheckBox ID="cbActive" runat="server" Checked='&lt;%# GenerateBindString(Container.DataItem) %&gt;' /&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/telerik:GridTemplateColumn&gt;
              &lt;telerik:GridTemplateColumn HeaderText="Status" SortExpression="Active" UniqueName="Active" ItemStyle-Width="100"&gt; 
                  &lt;ItemTemplate&gt; 
                      &lt;asp:Label ID="lblActive" runat="server" Text='&lt;%# Convert.ToBoolean(GenerateBindString(Container.DataItem)) == true ? "Active" : "Inactive" %&gt;'&gt;&lt;/asp:Label&gt; 
                  &lt;/ItemTemplate&gt; 
              &lt;/telerik:GridTemplateColumn&gt; 
           &lt;/Columns&gt;
      &lt;/MasterTableView&gt;
      &lt;ValidationSettings CommandsToValidate="PerformInsert,Update" /&gt;
      &lt;ClientSettings&gt;
          &lt;Selecting AllowRowSelect="True" /&gt;
      &lt;/ClientSettings&gt;
  &lt;/telerik:RadGrid&gt;
  
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:LBCust %&gt;" 
      SelectCommand="SELECT * FROM [LBX_Portal_AccountNumbers] WHERE ([Site] = @Site) AND (Active=@Active OR @Active = '0') ORDER BY AccountNumber"
      DeleteCommand="DELETE FROM [LBX_Portal_AccountNumbers] WHERE [ID] = @ID" 
      InsertCommand="INSERT INTO [LBX_Portal_AccountNumbers] ([AccountNumber], [Site], [Active]) VALUES (@AccountNumber, @Site, @Active)" 
      UpdateCommand="UPDATE [LBX_Portal_AccountNumbers] SET [AccountNumber] = @AccountNumber,  [Active] = @Active WHERE [ID] = @ID"&gt;
      &lt;SelectParameters&gt;
          &lt;asp:ControlParameter ControlID="dd_Status" Name="Active" 
              PropertyName="SelectedValue" Type="String" /&gt;
      &lt;/SelectParameters&gt;
      &lt;DeleteParameters&gt;
          &lt;asp:Parameter Name="ID" Type="Int32" /&gt;
      &lt;/DeleteParameters&gt;
      &lt;InsertParameters&gt;
          &lt;asp:Parameter Name="AccountNumber" Type="String" /&gt;
          &lt;asp:Parameter Name="Site" Type="String" /&gt;
          &lt;asp:Parameter Name="Active" Type="Boolean" /&gt;
      &lt;/InsertParameters&gt;
      &lt;UpdateParameters&gt;
          &lt;asp:Parameter Name="AccountNumber" Type="String" /&gt;
          &lt;asp:Parameter Name="Active" Type="Boolean" /&gt;
      &lt;/UpdateParameters&gt;
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>CS:</p>
  
  <pre><code>public partial class Admin_CustomerAccountManager : System.Web.UI.Page
  {
  public string SiteName = WebConfigurationManager.AppSettings["Site"].ToString();
  
  protected void Page_Load(object sender, EventArgs e)
  {
      Label LBXTitle = (Label)Master.FindControl("lbxTitle");
  
      LBXTitle.Text = "Customer Sapphire Account Number Manager";
  
      if (!this.IsPostBack)
      {
          SqlDataSource1.SelectParameters.Add("Site", DbType.String, SiteName);
      }
  }
  
  protected void dd_Status_SelectedIndexChanged(object sender, EventArgs e)
  {
      RadGrid1.DataBind();
  }
  
  protected bool GenerateBindString(object dataItem)
  {
      bool ret = false;
      // if column is null set checkbox.checked = false
  
      if ((DataBinder.Eval(dataItem, "Active")).ToString() == "")
          ret = false;
      else // set checkbox.checked to boolean value in Status column
          ret = (bool)DataBinder.Eval(dataItem, "Active");
  
      return ret;
  }
  
  protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
  {
      Page.Validate();
      if (Page.IsValid)
      {
          if (e.CommandName == "PerformInsert")
          {
              GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;
  
              Label lblID = (Label)gridEditFormItem.FindControl("lblID");
              TextBox txtAccountNumber = (TextBox)gridEditFormItem.FindControl("txtAccountNumber");
              CheckBox cbActive = (CheckBox)gridEditFormItem.FindControl("cbActive");
  
              bool isActive = true;
  
              string SqlStr = "INSERT INTO [LBX_Portal_AccountNumbers] ([AccountNumber], [Site], [Active])";
              SqlStr += " VALUES ('" + txtAccountNumber.Text + "'" + ", '" + SiteName + "'" + ", '" + isActive + "')";
  
              SqlDataSource1.InsertCommand = SqlStr;
              SqlDataSource1.Insert();
  
          }
  
          if (e.CommandName == "Update")
          {
              GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;
  
              Label lblID = (Label)gridEditFormItem.FindControl("lblID");
              TextBox txtAccountNumber = (TextBox)gridEditFormItem.FindControl("txtAccountNumber");
              CheckBox cbActive = (CheckBox)gridEditFormItem.FindControl("cbActive");
  
              bool isActive = false;
  
              if (cbActive.Checked)
                  isActive = true;
              else
                  isActive = false;
  
              string SqlStr = "UPDATE [LBX_Portal_AccountNumbers] SET [AccountNumber] = '" + txtAccountNumber.Text;
              SqlStr += "', [Active] = '" + isActive + "'  WHERE [ID] = " + lblID.Text;
  
              SqlDataSource1.UpdateCommand = SqlStr;
              SqlDataSource1.Update();
          }
      }
  }
  
  protected void cvAccountNumber_ServerValidate(object source, ServerValidateEventArgs args)
  {
      TextBox txtAccountNumber = RadGrid1.FindControl("txtAccountNumber") as TextBox;
      List&lt;GridDataItem&gt; Items = (from item in RadGrid1.Items.Cast&lt;GridDataItem&gt;()
                                  where item["AccountNumberView"].Text == txtAccountNumber.Text
                                  select item).ToList();
  
      args.IsValid = Items.Count() == 0;
  }
  }
  </code></pre>