Reputation: 46601
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
Upvotes: 0
Views: 3891
Reputation: 107536
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">
<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);
}
}
}
Upvotes: 1
Reputation: 9024
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!
Upvotes: 0
Reputation: 18181
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.
Upvotes: 0
Reputation: 14618
You should send a GridView reference to the control as a Property of the control (
public GridView GridViewToRebind {get; set;}
or something).
Upvotes: 1