【问题标题】:fire onClick event on ascx from aspx page c#从aspx页面c#在ascx上触发onClick事件
【发布时间】:2013-06-10 20:52:25
【问题描述】:

我在加载到 aspx 页面的动态用户控件上有保存按钮,但我想将按钮移动到 .aspx 页面上。如何将 onclick 事件从 aspx 触发到 ascx。

任何帮助都会很棒。

干杯。

代码示例:

ascx:

   protected void BT_Save_Click(object sender, EventArgs e)
{//Save details currently on ascx page }

aspx:

 protected void BT_aspx_Click(object sender, EventArgs e)
{
//when this button is clicked I need it to fire BT_Save_Click on ascx page to save the data
}

【问题讨论】:

  • 您需要在这个问题上付出更多的努力。非常不清楚您在问什么,我们没有上下文代码。
  • @Exor 如果触摸屏很快就会有 onLick 事件,我不会感到惊讶!! ;-P
  • @ShadowWizard 哈哈...... :D
  • 哈哈抱歉下次加码试试

标签: c# asp.net .net


【解决方案1】:

在用户控制中

<asp:Button ID="Button1" runat="server" Text="Button"  OnClick="Button1_Click"/>

在用户控件 .cs 页面中

public event EventHandler ButtonClickDemo;
protected void Button1_Click(object sender, EventArgs e)
{
    ButtonClickDemo(sender, e);
}

In Page aspx 页面

<uc1:WebUserControl runat="server" id="WebUserControl" />

在 Page.cs 中

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);
}

protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
{
    Response.Write("It's working");
}

【讨论】:

    【解决方案2】:

    在 ASCX 上写一个 public/internal sub,然后从 ASPX 上的 onClick 调用它?

    【讨论】:

    • 谢谢马丁,你的权利。让这比我需要的更复杂。再次感谢。
    【解决方案3】:

    您需要在 ascx 用户控件上创建一个自定义事件并在您的 aspx 页面中订阅它。

    看看这个问题

    define Custom Event for WebControl in asp.net

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      相关资源
      最近更新 更多