2

I need to add controls to a GridView dynamically, so I added a PlaceHolder, but it it giving me an error.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    PlaceHolder plachldr = e.Row.FindControl("PlaceHolder2") as PlaceHolder;
    Button btn = new Button() { ID = "btnShhow", Text = "Show" };
    plachldr.Controls.Add(btn);

    PlaceHolder placeholder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
    TextBox txt1 = new TextBox();
    placeholder.Controls.Add(txt1);
}

While adding the control to the PlaceHolder, is is giving me the following error:

Object reference not set to an instance of an object.

Here's the markup for my GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" onrowdatabound="GridView1_RowDataBound">    
    <Columns>  
        <asp:BoundField DataField="Name" HeaderText="Name" />  
        <asp:BoundField DataField="Salary" HeaderText="Salary" />      
        <asp:TemplateField>
            <ItemTemplate>  
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            </ItemTemplate> 
        </asp:TemplateField>      
        <asp:TemplateField>  
            <ItemTemplate>  
                <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>     
            </ItemTemplate>  
        </asp:TemplateField>      
    </Columns>
</asp:GridView>
2
  • You probably need to check for what type the row is - unless you have PlaceHolder2 in all the different row-types you can have within a GridView. So something like this: if (e.Row.RowType == DataControlRowType.DataRow) // your placeholder code here Commented Sep 8, 2011 at 6:30
  • @Pawan Bhise - is you issue resolved Commented Sep 8, 2011 at 12:33

1 Answer 1

1

You need to check plachldr or placeholder is null or not and also check for the RowType

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

  if( if (e.Row.RowType == DataControlRowType.DataRow) 
  {
    PlaceHolder plachldr = e.Row.FindControl("PlaceHolder2") as PlaceHolder;
    if(plachldr!=null)
    {
     Button btn = new Button() { ID = "btnShhow", Text = "Show" };
     plachldr.Controls.Add(btn);
    }

    PlaceHolder placeholder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
    if(placeholder!=null)
    {
     TextBox txt1 = new TextBox();
     placeholder.Controls.Add(txt1);
    }
   }

}
Sign up to request clarification or add additional context in comments.

3 Comments

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Salary" HeaderText="Salary" /> <asp:TemplateField> <ItemTemplate> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
/ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Use breakpoint in your binding method, and use quick watch, and go thru e.Row.Controls -- in order to see where is your placeholder, its ID. and how to get placeholder object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.