【问题标题】:How to access one usercontrol to another control using c#?如何使用 c# 访问一个用户控件到另一个控件?
【发布时间】:2015-07-10 09:04:09
【问题描述】:

我有两个用户控制器 test1.ascx 和 test2.ascx。如何在 test2.ascx 中访问 test1.ascx。

示例: test1.ascx.cs 我有以下方法:

Public string test()
{
   return "Welcome to user control 1";
}

在 test2.axcs.cs 中

public void Page_Load(object obj, sender e)
    {
       test1();
    }

public string test1()
   {
        return "Welcome to user control 2";
   }

我想访问 test1.ascx.cs 中的第二个用户控件(test2.ascx.cs)。

请帮我解决。

【问题讨论】:

  • 如何从我的 test1.ascx 调用 call test2.ascx page_load 事件。在我的父页面中是 abc.aspx。所以,我在我的 abc.aspx 父页面中注册了 test1.ascx。

标签: c#


【解决方案1】:

您可以在 test1.ascx 中注册 test2.ascx test1.ascx

<%@ Register Src="test2.ascx" TagName="Temp2" TagPrefix="uc2" %>
<uc2:Temp2 ID="Temp21" runat="server" />

test1.ascx.cs

Public string test()
{
   return Temp21.test1; // The output will be "Welcome to user control 2"
}

【讨论】:

  • 如果可以在 test1.ascx.cs 中调用 test2.ascx.cs page_load 事件。
  • 我认为这不是一个好主意。您如何将其封装在一个公共方法中并通过 test1.ascx.cs 调用它。我认为 Microsoft 设置该规则(Page_Load 受到保护)是有考虑的。
  • else 如果可以在 test1.ascx 中刷新 test2.ascx 用户控件。
  • 因为我在 test2.ascx 中有一个 telerik:listbbox。在 test1.ascx 中,单击保存按钮时,我必须刷新 test2.ascx 列表框并仅重新加载列表框项目,而无需重新加载整个页面。
  • 我之前说过,如果它是公共方法,你可以从任何 ascx 调用它(如果那些 ascxs 或其派生类在一页中)只需调用方法来刷新你的 Telerik 列表(在 test2 中)和调用保存按钮点击(test1)就像我之前发布的一样。
猜你喜欢
  • 1970-01-01
  • 2015-10-04
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多