[玻璃鱼V]嵌套用户控件间的调用问题

teita731 2009-03-01 07:06:11
页面page_A里使用了用户控件uc_A,用户控件uc_A中嵌套了用户控件uc_B,当触发了用户控件uc_B中的事件后需要调用uc_A中的方法以更新uc_A的内容.

我尝试在uc_B中使用这样的方法来达到目的:

protected void uc_B_Event(param param1)
{
... ...
Page p = this.Parent.Page;
UserControl uc = p.FindControl(uc_A) as UserControl;
Type t = uc.GetType();
MethodInfo mi = t.GetMethod(uc_A_Function);
if (mi != null)
{
mi.Invoke(uc, new object[] {});
}
}

错误提示:"System.NullReferenceException: 未将对象引用设置到对象的实例"
错误提示行:Type t = uc.GetType();

如何在嵌套用户控件中互相调用其方法呢?
...全文
130 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
teita731 2009-03-01
  • 打赏
  • 举报
回复
的确,1楼的代码通过检测.

谢谢了~
teita731 2009-03-01
  • 打赏
  • 举报
回复
但是我在uc_B中查看过this.Parent的值,是调用uc_A的页面page_A阿,并不是uc_A.
我姓区不姓区 2009-03-01
  • 打赏
  • 举报
回复
以上写法是针对Web Site项目,如果你的项目是WebApplication,那就更简单了:

protected void Button1_Click(object sender, EventArgs e)
{
if (this.Parent is WebUserControl1)
{
WebUserControl1 wuc = this.Parent as WebUserControl1;
wuc.SetText("Hello");
}
}
我姓区不姓区 2009-03-01
  • 打赏
  • 举报
回复
给你写个例子:
UserControl1:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="my" TagName="uc2" %>

<my:uc2 runat="server" ID="UC2" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

public void SetText(string str)
{
this.TextBox1.Text = str;
}
}


UserControl2:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />



public partial class WebUserControl2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
if (this.Parent is UserControl)
{
System.Reflection.MethodInfo mi = this.Parent.GetType().GetMethod("SetText", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (mi != null)
{
mi.Invoke(this.Parent, new object[] { "Hello" });
}
}
}
}


调用页:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="flash.aspx.cs" Inherits="WebApplication1.flash" %>
<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="my" TagName="uc1" %>
<!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>
<my:uc1 ID="UC1" runat="server"></my:uc1>
</div>
</form>
</body>
</html>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧