0

here my code-

GridView gvCondition = (GridView)this.FindControl("ucCondition").FindControl("gvCondition");
gvCondition.DataSource = objConditionFieldCollection;
gvCondition.DataBind();

but it is throwing as exception Object reference not set to an instance of an object.How can I access user control's gridview control from parent page?

ppp
  • 303
  • 2
  • 9
  • 22

1 Answers1

0

I don't know where you trying to access the UserControl's GridView, but why don't you simply expose a public property in the UserControl that returns the GridView(f.e. ConditionView)?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I have added property in user control but still it is not working `public GridView GetConditionGrid { get { return (GridView)this.FindControl("gvCondition"); } }` – ppp Feb 18 '11 at 13:12
  • @ppp: why do you serach for the controls via FindControl? You should be able to access the controls directly if not added dynamically. Where do you try to access the GridView(in which state of the page's life-cycle)? Are the usercontrol or the grid added dynamically to the page/usercontrol? – Tim Schmelter Feb 18 '11 at 13:19
  • I am trying to fing grid in a dropdown change event of parent page. I have tried to return grid control directly but that is also returning null.`if (this.gvCondition != null) return this.gvCondition; else return (GridView)this.FindControl("gvCondition");` Grid is not getting added dynamically. – ppp Feb 18 '11 at 13:31
  • @PPP: where is the UserControl added to the page, i mean is it nested in an other container control like FormView etc.? You should provide more source-code in your question(aspx). – Tim Schmelter Feb 18 '11 at 14:10