Click here to Skip to main content
13,593,025 members
Rate this:
Please Sign up or sign in to vote.
See more:
This button is user control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="user.ascx.cs" Inherits="Linque_Practice.user1" %>
<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" />

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 />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

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)
Posted 13-Jan-13 21:06pm
Updated 13-Jan-13 21:09pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180618.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100