Click here to Skip to main content
11,481,760 members (65,071 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP:NET
This button is user control
<%@ 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" />

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 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 22:06pm
v2
Rate this: bad
good
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.
  Permalink  
v2
Rate this: bad
good
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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