Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends.

I have Developed 2 UserControl and Loading it Dynamically. It Load Successfully but the Button Click Even of UserControl Does not Fire.

Below is My User Control Code.

C#
//AMSAssetAdd.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSAssetAdd.ascx.cs"Inherits="Website.App_WebControls.App_UserControls.AMSAssetAdd" %>
    <table>
<tr>
<td>     
        Asset Name  :
</td>
<td>

        <asp:TextBox ID="TxtAssetName " runat="server">
</td>
</tr>

<tr>
<td>     
        Asset Description    :
</td>
<td>

        <asp:TextBox ID="TxtAssetDescription " runat="server">
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>
</tr>
</table>

//AMSAssetAdd.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
    public partial class AMSAssetAdd : System.Web.UI.UserControl
    {
        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void SaveNext_Click(object sender, ImageClickEventArgs e)
        {         

         Control uc = LoadControl("AMSBrandAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);
        }
        #endregion


}
}


//AMSBrandAdd.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSBrandAdd.ascx.cs" Inherits="Website.App_WebControls.App_UserControls.AMSBrandAdd" %>
<table>
<tr>
<td>
 Brand Name  :
</td>
<td>
 <asp:TextBox ID="TxtBrandName" runat="server">
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>
</tr>
</table>  

////AMSBrandAdd.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
    public partial class AMSBrandAdd : System.Web.UI.UserControl
    {
    }
        protected void SaveNext_Click(object sender, ImageClickEventArgs e)
        {         

         Control uc = LoadControl("AMSUnitAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);
        }
}



This is my aspx page
<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="AssetMasterDetails.aspx.cs" Inherits="Website.ERP.AMS.AssetMasterDetails" EnableEventValidation="false" ValidateRequest="false" %>
<html>
<head>
<title></title>
</head>
<body>
<div>
 <asp:PlaceHolder ID="PlaceHolder1" runat="server">
</div>
</body>
</html>


//Code Behind
//AssetMasterDetails.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.BusinessLayer;
using Swash.Objects;
namespace Website.ERP.AMS
{
    public partial class AssetMasterDetails : System.Web.UI.Page
    {
     if(!IsPostback)
        {
         LoadControl();
        }
    }

void LoadControl()
    {
         Control uc = LoadControl("AMSAssetAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);

    }
}


While i Click Save and Next Button of AMSAssetAdd.ascx Button Click event Does not fire.

please give some suggestion.

your help will be highly appriciated.

Thanks & Regards

Rakesh Kumar Pattajoshi
Posted
Updated 23-Feb-12 19:35pm
v2
Comments
Anuja Pawar Indore 24-Feb-12 1:35am    
Why repost?

 
Share this answer
 
please check it out carefully ...

in AMSBrandAdd.ascx.cs
u have given button id as "SaveAsset"
in AMSBrandAdd.ascx.cs
u have written SaveNext_Click instead of SaveAsset_Click button functionality

Asset Description :
<asp:textbox id="TxtAssetDescription " runat="server" xmlns:asp="#unknown">
<asp:button id="SaveAsset" text="SAVE" runat="server" onclick="SaveAsset_Click" />
</asp:textbox>
 
Share this answer
 
Comments
rakesh pattajoshi 24-Feb-12 1:53am    
Sorry Here is My code

//AMSAssetAdd.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSAssetAdd.ascx.cs"Inherits="Website.App_WebControls.App_UserControls.AMSAssetAdd" %>
<table>
<tr>
<td>
Asset Name :
</td>
<td>

<asp:TextBox ID="TxtAssetName " runat="server">
</td>
</tr>

<tr>
<td>
Asset Description :
</td>
<td>

<asp:TextBox ID="TxtAssetDescription " runat="server">
</td>
</tr>

<tr>
<td>
</td>
<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>
</tr>

</table>

//AMSAssetAdd.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
public partial class AMSAssetAdd : System.Web.UI.UserControl
{
#region Events
protected void Page_Load(object sender, EventArgs e)
{

}

protected void SaveAsset_Click(object sender, ImageClickEventArgs e)
{

Control uc = LoadControl("AMSBrandAdd.ascx");
PlaceHolder1.Controls.Add(uc);
PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
PlaceHolder1.Control.Add(uc);
}
#endregion


}
}




//AMSBrandAdd.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSBrandAdd.ascx.cs" Inherits="Website.App_WebControls.App_UserControls.AMSBrandAdd" %>
<table>
<tr>
<td>
Brand Name :
</td>
<td>
<asp:TextBox ID="TxtBrandName" runat="server">
</td>
</tr>

<tr>
<td>
</td>
<td>
<asp:Button ID="SaveBrand" Text="SAVE" runat="server" onclick="SaveBrand_Click"/>
</td>
</tr>
</table>

////AMSBrandAdd.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
public partial class AMSBrandAdd : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void SaveBrand_Click(object sender, ImageClickEventArgs e)
{

Control uc = LoadControl("AMSUnitAdd.ascx");
PlaceHolder1.Controls.Add(uc);
PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
PlaceHolder1.Control.Add(uc);
}
}
}



This is my aspx page

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="AssetMasterDetails.aspx.cs" Inherits="Website.ERP.AMS.AssetMasterDetails" EnableEventValidation="false" ValidateRequest="false" %>
<html>
<head>
<title></title>
</head>
<body>
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</div>
</body>
</html>

//Code Behind
//AssetMasterDetails.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.BusinessLayer;
using Swash.Objects;

namespace Website.ERP.AMS
{
public partial class AssetMasterDetails : System.Web.UI.Page
{
if(!IsPostback)
{
LoadControl();
}
}

void LoadControl()
{
Control uc = LoadControl("AMSAssetAdd.ascx");
PlaceHolder1.Controls.Add(uc);
PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
PlaceHolder1.Control.Add(uc);

}
}
hi, check this code it will works fine.

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AMSBrandAdd.ascx.cs" Inherits="ASCX_AMSBrandAdd" %>
<table>
    <tr>
        <td>
            Brand Name :
        </td>
        <td>
            <asp:TextBox ID="TxtBrandName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="SaveBrand" Text="SAVE" runat="server" OnClick="SaveBrand_Click" />
        </td>
    </tr>
</table>
<div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</div>



AMSBrandAdd.ascx.cs


C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ASCX_AMSBrandAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SaveBrand_Click(object sender, EventArgs e)
    {


    }
}





AMSAssetAdd.ascx





XML
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AMSAssetAdd.ascx.cs" Inherits="ASCX_AMSAssetAdd" %>
<table>
    <tr>
        <td>
            Asset Name :
        </td>
        <td>
            <asp:TextBox ID="TxtAssetName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Asset Description :
        </td>
        <td>
            <asp:TextBox ID="TxtAssetDescription" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="SaveAsset" Text="SAVE" runat="server" OnClick="SaveAsset_Click" />
        </td>
    </tr>
</table>
<div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</div>


AMSAssetAdd.ascx.cs
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ASCX_AMSAssetAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
           if (!IsPostBack)
        {

        }
        else 
        {
            Control uc = LoadControl("../ASCX/AMSBrandAdd.ascx");
            PlaceHolder1.Controls.Add(uc);
        }
    }
    protected void SaveAsset_Click(object sender, EventArgs e)
    {
        

    }
}


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

<!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:PlaceHolder ID="PlaceHolder1" runat="server"/>
        </div>
    </form>
</body>
</html>



AssetMasterDetails.aspx.cs
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ASPX_AssetMasterDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

            LoadControl();

    }

    private void LoadControl()
    {
        Control uc = LoadControl("../ASCX/AMSAssetAdd.ascx");
         PlaceHolder1.Controls.Add(uc);



    }
}
 
Share this answer
 
Comments
rakesh pattajoshi 24-Feb-12 2:59am    
Thank's u for Giving me a solution, but still i am facing same problem
if i Load AMSAssetAdd.ascx from PreInit of .aspx pge,Button click of AMSAssetAdd.ascx fired and AMSBrandAdd.ascx is loading,but Button Click Event of AMSBrandAdd.ascx is not Firing.
Sanjeev Alamuri 24-Feb-12 4:49am    
controls will load every time if u add controls to aspx page dynamically . please check my code "AMSAssetAdd.ascx.cs". i just removed the code form save button click and placed in postback loop.

if (!IsPostBack)
{

}
else
{
Control uc = LoadControl("../ASCX/AMSBrandAdd.ascx");
PlaceHolder1.Controls.Add(uc);
}

now its working fine for Button Click Event of AMSBrandAdd.ascx.

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


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