如何从usercontrole调用按钮单击事件到后面的代码(.cs文件) [英] How to invoke the button click event from usercontrole to in code behind (.cs file)

查看:106
本文介绍了如何从usercontrole调用按钮单击事件到后面的代码(.cs文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此按钮是用户控件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="user.ascx.cs" Inherits="Linque_Practice.user1" %>
Basheer
<p>
    &nbsp;</p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" 

    Width="64px" />

    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Delete" 

    Width="64px" />





在aspx页面中我有三个文本框





In aspx page i have three textbox

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
second<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    add
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>





i希望当我点击添加按钮时,在usercontrole中它应该添加n来自前2个文本的umber和第3个文本框中的结果存储



如何从usercontrole调用按钮单击事件到后面的代码(.cs文件)



i want want when i click the add button its in usercontrole it should add the number from first 2 text and result store in 3rd text box

How to invoke the button click event from usercontrole to in code behind (.cs file)

推荐答案

框架有一种方法可以通过一些约定来解决这个问题。



您通常会声明一个公共委托并定义一个使用该委托在您的用户控件中的公共事件。在控件标记中使用 OnEventName =PageMethod。其中 EventName 是usercontrol中定义的事件的名称, PageMethod 是aspx页面中的方法。从用户控件代码触发事件。



说代理如下:

The framework has a way of wiring this through some convention.

You normally declare a public delegate and define a public event in your user control using that delegate. In the control markup use OnEventName="PageMethod". Where EventName is the name of the event defined in the usercontrol and PageMethod is the method in the aspx page. Trigger the event from user control code-behind.

Say delegate is as follows:
public delegate void ClickHandler();





用户控件类中的事件定义如下:



Event definition inside user control class is as follows:

public event ClickHandler Clicked;





编写触发事件的代码。在这种情况下按钮点击:



Write code which fires the event. In this case button click:

public void btnSubmit_Click(object s, EventArgs e)
{
if(Clicked != null) Clicked();
}





最后标记:



Finally the markup:

<cc1:uc1 id="uc1"  runat="server"  önClicked="UcClicked"></cc1:uc1>





确保页面上有一个方法页面;它不应该是私有的。



Make sure there exists a page a method on the page; it should not be private.


代码可以像一样简单点击(o,null)其中o是对象和 null 我们在通过后面的代码触发事件时可能不需要的事件参数。
Code could be as simple as Click(o, null) where o is the object and null the event argument which we may not need in case of firing the event via code behind.


这篇关于如何从usercontrole调用按钮单击事件到后面的代码(.cs文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆