Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a custom control, following is the code of class file:-

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;
using System;

namespace WebControls
{
    [ParseChildren(true)]
    [PersistChildren(true )]
    public class ECBDiv : WebControl
    {
        public ECBDiv()
        {
        }

        private ITemplate div = null;

        DivContainer containerDiv = new DivContainer();

        [TemplateContainer(typeof(DivContainer))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        public ITemplate Div
        {
            get
            {
                return div;
            }
            set
            {
                div = value;
                div.InstantiateIn(containerDiv);


            }
        }
        public class DivContainer : Control, INamingContainer
        {
            internal DivContainer()
            {
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Indent++;
            writer.WriteBeginTag("div");
            writer.WriteAttribute("id", "Box");
            writer.WriteAttribute("class", "right");
            writer.WriteAttribute("runat", "server");
            writer.Write((HtmlTextWriter.TagRightChar));
            containerDiv.RenderControl(writer);
            writer.WriteLine("");
            writer.WriteEndTag("div");
        }
    }

}

///////////End of code- ECBDiv/////////////

Now I am using this control on my aspx page. I am putting a textbox and a command button inside the DIV Template on the aspx page. But i am not getting the button click event on the code behind. I am getting textbox and it s properties but the button click event is not getting fired. I have also tried button.Click+= new EventHandler(BtnClicked); But this also is not working.

Anyone can guide me??
Posted
Updated 2-Jun-10 23:46pm
v2

1 solution

Change your existing code for the Div property as following.

public ITemplate Div
       {
           get
           {
               return div;
           }
           set
           {
              div = value;
              Controls.Clear();
              div.InstantiateIn(containerDiv);
              base.Controls.Add(containerDiv);

           }
       }



You have missed adding the container to the base class.

base.Controls.Add(containerDiv);



And your HTML within the Div will look like.

<cc1:ECBDiv ID="ECBDiv1" runat="server">
            <Div>
                <asp:button id="BtnTest" text="Test" runat="server" onclick="BtnTestClick" xmlns:asp="#unknown" />
            </Div>
        </cc1:ECBDiv>



And finally your event

protected void BtnTestClick(object sender, EventArgs e)
        {

            Response.Write("Test");
        }
 
Share this answer
 
v3
Comments
Mani1981 4-Jun-10 4:09am    
Thanks Prakash, it worked perfectly. Thanks a ton:)
Mani1981 10-Jun-10 2:14am    
Hey Prakash, I want to createajax modal popup template based custom control. It should work the same way the previous control worked . But i am not able to create asp server controls in the custom class. can u help me?
Mani1981 10-Jun-10 2:38am    
the main problem i am facing is how to create asp controls like button, panel and ModalPopupExtender in the class file. In the previous case all the controls were html control so i created them by overriding the Render Event and using HtmlTextWriter object.

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