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.
我有一个aspx页面,它有一个GridView和一个带有摘要数据的Panel。摘要数据位于控件中,GridView位于aspx页面上。当有人单击摘要数据中的项目时,我想重新绑定GridView,但我无法弄清楚如何从控件中执行此操作。
I tried using FindControl, but probably did not use it correctly.
我尝试使用FindControl,但可能没有正确使用它。
I tried this with FindControl, but it didn't work:
我用FindControl尝试了这个,但它不起作用:
GridView gv = (GridView)FindControl("gvSearchResults"); //returns null
4 个解决方案
#1
You should send a GridView reference to the control as a Property of the control (
您应该将GridView引用作为控件的属性发送到控件(
public GridView GridViewToRebind {get; set;}
or something).
#2
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().
您的摘要数据属于什么样的控制?也许您可以将EventHandler添加到您点击项目时触发的摘要数据控件。您可以在.aspx代码隐藏中编写事件的处理程序,然后在.aspx的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);
}
}
}
#3
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.
容器使用FindControl来查找它包含的子控件,如果使用Page对象来查找gridview,您应该能够找到它。你如何使用FindControl方法?你能发一些简单的代码吗?
#4
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:
如果你的所有控件都在同一页面上,那么你应该看到GridView(在intellisense中,来自它被调用的事件),而不必使用FindControl。如果您的summarydata位于单独的UserControl中,请按照以下步骤操作:
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!
从UserControl,你的摘要数据在哪里,引用父页面(让我们说它是SomePage.aspx):SomePage parent =(SomePage)this.Page;在SomePage.aspx代码隐藏中,放入一个重新绑定的方法:public void Rebind(){gridview1.Bind();然后你可以从用户控件调用Rebind方法:parent.Rebind()你也可以添加一些参数或一些其他UI / biz逻辑!
#1
You should send a GridView reference to the control as a Property of the control (
您应该将GridView引用作为控件的属性发送到控件(
public GridView GridViewToRebind {get; set;}
or something).
#2
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().
您的摘要数据属于什么样的控制?也许您可以将EventHandler添加到您点击项目时触发的摘要数据控件。您可以在.aspx代码隐藏中编写事件的处理程序,然后在.aspx的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);
}
}
}
#3
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.
容器使用FindControl来查找它包含的子控件,如果使用Page对象来查找gridview,您应该能够找到它。你如何使用FindControl方法?你能发一些简单的代码吗?
#4
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:
如果你的所有控件都在同一页面上,那么你应该看到GridView(在intellisense中,来自它被调用的事件),而不必使用FindControl。如果您的summarydata位于单独的UserControl中,请按照以下步骤操作:
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!
从UserControl,你的摘要数据在哪里,引用父页面(让我们说它是SomePage.aspx):SomePage parent =(SomePage)this.Page;在SomePage.aspx代码隐藏中,放入一个重新绑定的方法:public void Rebind(){gridview1.Bind();然后你可以从用户控件调用Rebind方法:parent.Rebind()你也可以添加一些参数或一些其他UI / biz逻辑!