tags:

views:

1031

answers:

4

I have an aspx page which has a GridView and a Panel with Summary Data. The Summary Data is in a control and the GridView is on the aspx page. When someone clicks an item in the Summary Data, I want to rebind the GridView, but I can't figure out how to do it from the control.

I tried using FindControl, but probably did not use it correctly.

I tried this with FindControl, but it didn't work:

GridView gv = (GridView)FindControl("gvSearchResults"); //returns null
+1  A: 

You should send a GridView reference to the control as a Property of the control (

public GridView GridViewToRebind {get; set;}

or something).

Adam A
Can you provide an example?
Xaisoft
I put this in the aspx page as a GridView and I guess I need to put a property in the control to retrieve the GridView as well.
Xaisoft
I can't figure out how to use FindControl to get the GridView.
Xaisoft
i think Adam is saying that you put this property in the CONTROL and the page (that i presume can see the control) calls the CONTROL's new property passing the Gridview
Exactly what p4b said. The page knows it has a GridView. So somewhere early in the Page lifecycle, you can say myControl.GridViewToRebind = _theGridView;Then in the control you can reference the GridView using GridViewToRebind
Adam A
A: 

FindControl is used by the container to find the child control it contains, if you use the Page object to find the gridview, you should be able to find it. How do you use your FindControl method? Can you post some simple code.

J.W.
Can you provide an example of using the Page to find the GridView?
Xaisoft
A: 

If all of your controls are on the same page, then your should see GridView (in intellisense, from event that its called), and do not have to use FindControl. If your summarydata is in separate UserControl, follow this steps:

From UserControl, where is your Summary Data, reference parent page (let say it's SomePage.aspx):
SomePage parent=(SomePage)this.Page;
in SomePage.aspx codebehind, put one method that does rebinding:
public void Rebind() { gridview1.Bind(); }
then you can call that Rebind method from your user control: parent.Rebind()
you can also add some parameters or some other UI/biz logic to it!

Hrvoje
+1  A: 

What kind of control is your Summary Data in? Maybe you could add an EventHandler to your Summary Data control that fires when you click on an item. You would write the handler for the event in your .aspx code-behind, and then link them up in your .aspx's Page_Load().

Here's a quick example:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewEventHandling._Default" %>
<%@ Register TagName="MyControl" TagPrefix="mc" Src="~/SampleData.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="uxGridView" runat="server" AutoGenerateColumns="true">
        </asp:GridView>

        <mc:MyControl ID="myControl" runat="server" />

    </div>
    </form>
</body>
</html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GridViewEventHandling
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            myControl.OnLinkClick += new EventHandler(myControl_OnLinkClick);
        }

        private void myControl_OnLinkClick(object sender, EventArgs e)
        {
            uxGridView.DataSource = GetDataSource();
            uxGridView.DataBind();
        }

        private IDictionary<string, string> GetDataSource()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Product 1", "Description 1");
            dict.Add("Product 2", "Description 2");
            dict.Add("Product 3", "Description 3");
            return dict;
        }


    }
}

SampleData.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SampleData.ascx.cs" Inherits="GridViewEventHandling.SampleData" %>

<asp:LinkButton ID="item1" OnClick="HandleClick" CommandName="BindGrid" CommandArgument="1" Text="Item 1" runat="server" /><br />
<asp:LinkButton ID="item2" OnClick="HandleClick" CommandName="BindGrid" CommandArgument="2" Text="Item 2" runat="server" /><br />
<asp:LinkButton ID="item3" OnClick="HandleClick" CommandName="BindGrid" CommandArgument="3" Text="Item 3" runat="server" /><br />

SampleData.ascx.cs:

using System;

namespace GridViewEventHandling
{
    public partial class SampleData : System.Web.UI.UserControl
    {
        public event EventHandler OnLinkClick;

        protected void HandleClick(object sender, EventArgs args)
        {
            if (OnLinkClick != null)
                OnLinkClick(sender, args);
        }
    }
}
Cory Larson
This is how I would do it, the logic to refresh the gridview really belongs in the page since that is where it resides. Doing it from the control isn't ideal.
spilliton