4

I have a form view with an item template with a control inside, is it possible to access that control OnDatabound so I can bind the control with data. I'm using a panel as an example here.

<cc1:LOEDFormView ID="FireFormView" runat="server" DataSourceID="DataSourceResults"     CssClass="EditForm" DataKeyNames="id" OnDatabound="FireFromView_Databound">
<ItemTemplate>

<asp:Panel ID ="pnl" runat="server"></asp:Panel>

</ItemTemplate>

</cc1:LOEDFormView>
Funky
  • 12,890
  • 35
  • 106
  • 161

4 Answers4

10

You have to take care the item mode as well in which your control exist which you want to find. Like if your control in Item Template, then it would be like..

if (FormView1.CurrentMode == FormViewMode.ReadOnly)
{

  Panel pnl = (Panel)FormView1.FindControl("pnl");
}
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 2
    Make sure formview has row databound otherwise it will always be null. Check your select statement and parameters as well – irfandar Oct 20 '12 at 17:07
  • I'm trying to findControl in the OnDataBound event method. And it's still null. I found via debugging that it is NOT null, when I call FormView.DataBind() manually from any location (page_load, etc). But when the SqlDataSource autobinds via the SqlDataSourceID attribute, no controls exist within the FormView in the OnDataBound. RIDICULOUS. About to scrap this entire thing and just use razor mvc which isn't retarded. – Barry Sep 21 '17 at 14:42
  • This is an old question, but for more recent code, try looking for `FormView1.Row.FindControl("pnl")` instead. – Paul Apr 12 '18 at 10:17
1

This code below solved my issue. Although the example accesses a label it applies to most controls. You simply have to add a DataBound event to your FormView.

protected void FormView1_DataBound(object sender, EventArgs e)
{
  //check the formview mode 
  if (FormView1.CurrentMode == FormViewMode.ReadOnly)
  {
    //Check the RowType to where the Control is placed
    if (FormView1.Row.RowType == DataControlRowType.DataRow)
    {
      Label label1 = (Label)UserProfileFormView.Row.Cells[0].FindControl("Label1");
      if (label1 != null)
      {
        label1.Text = "Your text";
      }
    }
  }
}
benscabbia
  • 17,592
  • 13
  • 51
  • 62
1

I don't see a label in your markup but see a Panel. So to access the panel,

Try

Panel p = FireFormView.FindControl("pnl") as Panel;
if(p != null)
{
    ...
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
0
    if (FireFormView.Row != null)
    {
        if (FireFormView.CurrentMode == FormViewMode.ReadOnly)
        {
            Panel pnl = (Panel)FireFormView.FindControl("pnl");
        }
        else
        {
            //FormView is not in readonly mode
        }
    }
    else
    {
        //formview not databound, check select statement and parameters.
    }
irfandar
  • 1,690
  • 1
  • 23
  • 24