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

FindControl returning null.

2 Answers 312 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roland
Top achievements
Rank 1
Roland asked on 15 Oct 2010, 06:40 PM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Oct 2010, 08:01 AM
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.
0
Roland
Top achievements
Rank 1
answered on 18 Oct 2010, 06:22 PM
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.
Tags
Grid
Asked by
Roland
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Roland
Top achievements
Rank 1
Share this question
or