ASP.NET Coding the OnClick Event …

Instead of placing controls on an .aspx page, I tend to create them in a C# class. For me, this architecture not only facilitates code reuse, but makes complex web applications easy to manage. One aspect of this architecture that is always problematic is event handling. My latest project was no exception. Its really a memory issue. That is my memory. Like I can’t remember from project to project the correct C# syntax.

The answer is really simple. Lets say you have a control with an ID of "ConcelNewRecord". When clicked, you want to cancel a pending action. Just code the control "Click" property as follows;

CancelNewRecord.Click +=  new EventHandler(this.CancelNewRecord_Click);

You will also need to code the event actions, e.g.;

protected void CancelNewRecord_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        Panel Panel1 = (Panel)    lb.Parent.Parent.Parent.Parent;
        Panel1.Visible = false;
        lb = (LinkButton)Panel1.FindControl("NewRecord");
        if(lb != null)
            lb.Visible = true;
    }

The above code is encapsulated in a class that is instantiated from an aspx.cs page.

2 thoughts on “ASP.NET Coding the OnClick Event …

Leave a reply to Submit Resume Cancel reply