Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,,

I have one button control in user control ,i have taken button control in .aspx page and i want to do postback for button control in .aspx page?how to do this can any one help me..?ASAP...

Thanks In advance
Posted
Updated 21-Jun-12 23:00pm
v2
Comments
Vani Kulkarni 22-Jun-12 5:03am    
Do you mean that you want to perform any operation on its Button Click?
Ganesh_mca 22-Jun-12 5:06am    
yes ..only usercontrol button.

When there is a submit action on usercontrol, whole page will be submitted and it will also construct the 'aspx' again.
In simple terms on what you can easily see, put a break point on the page_load methods of main aspx page and usercontrol ascx page. See on button click all the page loads are called for that page.

Look here and see how page life cycle works:
Complete Lifecycle of an ASP.Net page and controls[^]
ASP.NET Application and Page Life Cycle[^]
MSDN: ASP.NET Page Life Cycle Overview[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 22-Jun-12 6:00am    
My 5!
Sandeep Mewara 22-Jun-12 14:02pm    
Thanks.
Manas Bhardwaj 22-Jun-12 7:22am    
Correct +5!
Sandeep Mewara 22-Jun-12 14:02pm    
Thanks.
XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Dalegate_WebUserControl" %>

<asp:Button ID="btnTest" runat="server" Text="I am Inside User Control" OnClick="btnTest_Click" />

Fig – (1) WebUserControl.ascx

            On WebUserControl.ascx.cs I have written simple delegate and event handler as shown below,

public partial class Dalegate_WebUserControl : System.Web.UI.UserControl
{

    // Delegate declaration
    public delegate void OnButtonClick(string strValue);

    // Event declaration
    public event OnButtonClick btnHandler;

    // Page load
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
           // Check if event is null
           if (btnHandler != null)
               btnHandler(string.Empty);

           // Write some text to output
           Response.Write("User Control’s Button Click <BR/>");
    }
}

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Dalegate_Default" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1″ %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1″ runat="server">
    <div>
        <asp:Label ID="lblText" Text="I am On Main Page : "
                   runat="server"></asp:Label>
        <asp:DropDownList ID="ddlTemp" runat="server">
            <asp:ListItem>Chirag</asp:ListItem>
            <asp:ListItem>Dipak</asp:ListItem>
            <asp:ListItem>Shailesh</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <uc1:WebUserControl ID="WebUserControl1″ runat="server" />

    </div>
    </form>
</body>
</html>

Fig – (3) Default.aspx

Default.aspx has one drop down list and a user control as shown in above code. Lets look at cs file,

C#
public partial class Dalegate_Default : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
      {
             // Declare and Define Event of User Control. When User Clicks on button
             (which is inside UserControl)
             // below event is raised as I have called raised that event on Button Click
             WebUserControl1.btnHandler += new
             Dalegate_WebUserControl.OnButtonClick(WebUserControl1_btnHandler);

      }

      void WebUserControl1_btnHandler(string strValue)
      {
             Response.Write("Main Page Event<BR/>Selected Value: " +
                                     ddlTemp.SelectedItem.Text + "<BR/>");
      }
}
 
Share this answer
 
v2
add new class in appcode

C#
public class ClsUserControl : UserControl
{
    public delegate void EventHandler(object sender, EventArgs e);
    public event EventHandler UserBtnClick;

    public ClsUserControl()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void UserBtn(object sender, EventArgs e)
    {
        if (UserBtnClick != null) UserBtnClick(sender, e);
        FunUserBtnClick(sender);
    }
    public virtual void FunUserBtnClick(object sender)
    {
        //do nothing
    }
}


add a user control with button

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


in control cs add this line

C#
public partial class WebUserControl : ClsUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserBtn(sender, e);
    }
}



in your aspx page add user control like this

ASP.NET
<uc1:webusercontrol id="WebUserControl1" runat="server" onuserbtnclick="UserControl_Click" xmlns:uc1="#unknown" />


in your this aspx page cs add your code for this event

C#
public void UserControl_Click(object sender, EventArgs e)
    {
        //Response.Write("comein");
        saveData();
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900