FindControl returning null.

3 posts, 0 answers
  1. Roland
    Roland avatar
    11 posts
    Member since:
    Mar 2007

    Posted 15 Oct 2010 Link to this post

    Hi people.

    Question - so I have a button inside a formtemplate in a radGrid. Based on selections in the formtemplate, hitting this button issues a "Generate" command which then the form posts back and goes to the database to retrieve the data for the specified selections.

    It works when you are in "insert" mode but the same FindControl method is not working when the row is an existing row and you are trying to update.

    Basically, the Insert/Update as well as this Generate command go off the ItemCommand event so u start off with

    GridEditableItem gei = e.Item as GridEditableItem;

    The FindControl method is then used for the various controls present in the FormTemplate afterwards.

    Am I missing something?

    Thanks.

    Roland

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Oct 2010 Link to this post

    Hello Roland,

    Set separate CommandName for 'Generate' button in edit and insert form. Then In ItemCommand event access the control by using corresponding grid item(GridEditFormItem/GridEditFormInsertItem). Check out the sample code below.

    ASPX:
    <FormTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind( "EmployeeID") %>'></asp:TextBox>
        <asp:Button ID="Button1" Text="Generate" runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "GenerateInsert" : "GenerateUpdate" %>'></asp:Button>
    </FormTemplate>

    C#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
      {
         if (e.CommandName == "GenerateUpdate") // 'Generate' button in edit form
          {
              GridEditFormItem editItem = (GridEditFormItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[e.Item.ItemIndex];
              TextBox txt = (TextBox)editItem.FindControl("TextBox1");// accessing TextBox in edit form
          }
          if (e.CommandName == "GenerateInsert")// 'Generate' button in insert form
          {
              GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
              TextBox txt = (TextBox)insertItem.FindControl("TextBox1");// accessing TextBox in insert form
          }
      }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Roland
    Roland avatar
    11 posts
    Member since:
    Mar 2007

    Posted 18 Oct 2010 Link to this post

    Thanks for the reply, Princy. Ill give that a shot and update with results.

    UPDATE:
    Yes, specifying separate commandname for handling in ItemCommand Event and using appropriate GridItem works.
Back to Top