Ask Dr. Dotnetsky - Web control library button click event does not fire  Ask Dr. Dotnetsky - Web control library button click event does not fire

Asked By Yanis Belgaid
19-Sep-07 07:13 AM

Hi,

I have created a web control library project.

I created a button as follows:

Button btnAssign = new Button();

 btnAssign.CausesValidation = false;

 btnAssign.ID = "As1";

 btnAssign.Text = "...";

 btnAssign.Font.Name = "Arial";

 btnAssign.ForeColor = Color.Gray;

 btnAssign.Click += new EventHandler(this.btnAssign_Click);

and added it into a panel myPanel

myPanel.Controls.Add(btnAssign);

 

The rendering was done as follows:

protected override void RenderContents(HtmlTextWriter output)

 {

            AddAttributesToRender(output);

            myPanel.RenderControl(output);

}

 

 

 

 

I have added a trace just to see whether it enters btnAssign_Click or not

private void btnAssign_Click(object sender, System.EventArgs e)

        {

        Page.Trace.Warn("btnAssign_Click");

}

I created a web project to test the control and set the trace to true:

 

protected void Page_Load(object sender, EventArgs e)

    {

        WCL.myClass dc = new WCL. myClass();

         //Add to phDC place holder

        phDC.Controls.Add(dc);

 

    }

When the test page is loaded the button from the web control library appears.

When I click it nothing happens (does not enter btnAssign_Click).

Is there anything else I need to do?

Please help

Thanks

Are you recreating the button on post back?  Are you recreating the button on post back?

19-Sep-07 09:05 AM
Dynamically created controls always have to be created even on post pack.

Yes I am creating the control even on post back  Yes I am creating the control even on post back

20-Sep-07 04:06 AM

Unfortunately the even is not firing.

Check this article on dynamic controls out  Check this article on dynamic controls out

20-Sep-07 12:20 PM

Make sure you are wiring things up in the proper place.

http://www.eggheadcafe.com/articles/extendtextboxviewstate.asp

Create New Account
help
informed that this control has no CausesValidation property and hence it will not validate the page before submitting. [CODE] using System; using System.IO; using System.Text; using System.Collections; using namespace CustomControls { / / / <summary> / / / Summary description for LinkButtonEx. / / / < / summary> public class LinkButtonEx:HyperLink, IPostBackEventHandler { public event EventHandler Click; public event EventHandler DoubleClick; public LinkButtonEx():base() { } protected override void OnPreRender(EventArgs e) { base.OnPreRender (e); } protected override void AddAttributesToRender(HtmlTextWriter writer) { string strDoubleClickScript = this.Page.GetPostBackEventReference(this, "DOUBLE"); string strClickScript = this Page.GetPostBackEventReference(this); string strTimeOut = this.UniqueID + RandomString(5, true); writer.Write("<script language = \ "javascript \ "> var
the click event of the custom table cell control never gets fired (but postback to page is okay). I tried to compile the custom control in VS 2005 too, but same server> < / {0}:CustomTableCell> ")> _ Public Class CustomTableCell Inherits TableCell Implements IPostBackEventHandler Public Event Click As EventHandler Protected Overridable Sub OnClick(ByVal e As EventArgs) If ClickEvent IsNot Nothing Then RaiseEvent Click(Me, e) End If End Sub Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent OnClick(New EventArgs()) End Sub Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter) writer.AddAttribute("name", UniqueID) writer.AddAttribute("ID", UniqueID) writer.AddAttribute("onClick", Page.ClientScript.GetPostBackEventReference(Me, String.Empty)) MyBase.AddAttributesToRender(writer) End Sub End Class In order to create a Custom Event for a Custom
me the FAQs on Masterpages in asp.net. Thanks alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used