大概可以这么做(好处是控件一与控件二几乎是独立的,可以在其他地方重用):public class YourUserControl2: UserControl { public void BindData(int abc) { //绑定你的GridView,如果需要输入,就从参数输入 } } public class YourUserControl1: UserControl {
public event EventHandler DisplayData; public int SomeProperty { get {
} } public void Button_Click(Object sender, EventArgs e) { if (DisplayData != null) DisplayData(this,EventArgs.Empty); } }public class YourPage : Page {
{
int SomeMethod();
}class YourUserControl2 : UserControl, SomeInt
{}然后让你的页面把uc2以接口的方式传给你的uc1
想在页面中加载uc1和uc2,在uc1中的checkbox中选定值后,点击Button,在button_Click事件中调用uc2的GridView数据绑定。不知道这样是不是思路有问题?
{
public void BindData(int abc)
{
//绑定你的GridView,如果需要输入,就从参数输入
}
}
public class YourUserControl1: UserControl
{
public event EventHandler DisplayData; public int SomeProperty
{
get
{
}
} public void Button_Click(Object sender, EventArgs e)
{
if (DisplayData != null)
DisplayData(this,EventArgs.Empty);
}
}public class YourPage : Page
{
YourUserControl1 uc1;
YourUserControl2 uc2; //.....
void Page_Load(Object sender, EventArgs e)
{
uc2.DisplayData += BindDisplay;
} public void BindDisplay(Object sender, EventArgs e)
{
uc2.BindData(uc1.SomeProperty);
}
}
开始是提示 uc2中没有定义DisplayData. 我在public class YourUserControl2: UserControl 中加入了声明public event EventHandler DisplayData;
错误提示变成system.NullReferenceException, 好像是未将对象引用设置到对象的实例。不知问题在何处。请大侠再帮忙看看!
我在加载了2个控件的页面里,比如:
<% Register Src="..." ...>
<% Register Src="..." ...>
<uc1:xxx ID="viewCB" runat="server" />
<uc2:xxx ID="viewGV" runat="server">
我用ID值 viewCB 和 viewGV 替换你上面代码里的uc1和uc2,错误提示没了,可是 public void BindDisplay(Object sender, EventArgs e) 里的东西不运行。我设了停顿点,在uc1中,每点击按钮一次,运行一次 Page_Load 中的 uc2.DisplayData += BindDisplay; 但BindDisplay 中的 uc2.BindData(uc1.SomeProperty);不运行就直接结束了,也就是说无法完成绑定了。请教怎么解决呢?
感谢思归的耐心详细讲解,感动!!
结贴,给分!!!