Click here to Skip to main content
15,309,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi sir i am creating dynamically user control but button event is not firing please help me it is urgent.
Here is my code please help if any one have the solution thanks in advance.

ASP.NET
//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"></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>

C#
//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


}
}



ASP.NET
//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"></asp:TextBox>
</td>
</tr>

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

C#
////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
ASP.NET
<%@ 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"></asp:PlaceHolder>
</div>
</body>
</html>

C#
//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);

    }
}
Posted
Updated 23-Feb-12 19:50pm
v3
Comments
Martin Arapovic 23-Feb-12 14:35pm
   
Hi,
Can you post some code, so we can try to see whats wrong?! The common problem here is that controls are didn't created at right place within page life-cycle. Try to create yours controls within Page_Init handler because viewstate is loaded in Pre_Load handler which happens before Page_Load and ensure that controls have ID defined...
This is only my lucky guess. :)
Anuja Pawar Indore 24-Feb-12 1:32am
   
Delete code from the comment area
Tech Code Freak 24-Feb-12 1:51am
   
Corrected pre tags

Hi,

I'm not sure what you mean, but if it is dynamically created controls, then you need to make sure to re-create the button on postback. Otherwise the event will not get fired!

//Updated
C#
public partial class AssetMasterDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     //if(!IsPostback)
     //   {
         LoadControl();
     //   }
    }
    void LoadControl()
    {
         Control uc = LoadControl("AMSAssetAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         //PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         //PlaceHolder1.Control.Add(uc);
 
    }
}


There are code errors in your posting f.x. you declare PlaceHolder1 which is not possible in the scope in LoadControl, it's already declared, you add the uc twice to the placeholder, event name in code behind not same as event name in ascx and more :-)

Regards
Joachim
   
v6
Comments
[no name] 24-Feb-12 1:15am
   
Please sir check my code and reply.Thanks in Advance
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


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

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