Briskstar Technologies
Briskstar Technologies

Reputation: 2253

Access gridview as property from usercontrol

I have gridview bind in user control and want to access that gridview from user control in page and use export to excel.

I created property in usercontrol to access but I am not able to access it.

How can I access gridview from usercontrol in my page?

Upvotes: 0

Views: 1986

Answers (3)

Mojtaba Pourmirzaei
Mojtaba Pourmirzaei

Reputation: 308

if you use Master page for your aspx pages you can access your page GridView in your user control in this way

 Page mypage= this.Page;
  GridView mypageGridview = (GridView)mypage.Master.FindControl("ContentPlaceHolder1").FindControl("YourGridView");

if you have not master page

 Page mypage= this.Page;
    GridView mypageGridview = (GridView)mypage.FindControl("YourGridView");

Upvotes: 0

huMpty duMpty
huMpty duMpty

Reputation: 14470

I like using interface.

You can create an interface and implement it on the web page. And using a interface object in usercontrol, you can call the interface method.

eg, Interface

public interface IExport
{
    void Export();
}

Your web page

public partial class _Default : System.Web.UI.Page, IExport
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UC11.MyExport = this;
        //UC11 will be whatever name of your usercontrol
    }

    public void Export()
    {
        //your export code
    }
}

And your usercontorl

public partial class UC1 : System.Web.UI.UserControl
{
    public IExport MyExport { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        MyExport.Export();
    }
}

Hope this helps

Upvotes: 0

abuseukk
abuseukk

Reputation: 245

Why don't you write ExportToExcel method into the user control or just make a property which is public and return the refference to the grid:

public GridView MyGrid
{
    get{ return this.GridView1;}
}

Upvotes: 1

Related Questions