Programmatically add a linkbutton with onclick event

Posted on 2005-05-06
Last Modified: 2011-09-20
Its easy to write a linkbutton into an aspx page like so:

<asp:LinkButton ID="lbDate" runat="server" Text="Click Me" OnClick="lbDate_click" CommandArgument="1" />

But how do I dynamically create one?  The Onclick method is protected and cannot be set programmatically.  Here's what I'm trying to do...

[using vb]

For i = 0 To 9
  Dim lbDate As LinkButton = New LinkButton
  lbDate.ID = "lb" & i
  lbDate.Text = "Test " & i
  lbDate.Onclick = "lbDate_click"   'Doesn't work, onclick is protected.  HELP!
  lbDate.CommandArgument = i
Question by:osubvr
    LVL 6

    Expert Comment

    lbDate.Click += new System.EventHandler(this.lbDate_click);

    Author Comment

    Sorry, now I feel stupid.

    I neglected to mention that I'm using the .NET Framework 2.0beta.

    When I use your example, I get the following error:

    Compiler Error Message: BC32022: 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Source Error:

    Line 137:            lbDate.CommandArgument = DateAdd(DateInterval.Day, -i, Date.Today)
    Line 138:            lbDate.Text = DateAdd(DateInterval.Day, -i, Date.Today)
    Line 139:            lbDate.Click += New System.EventHandler(this.lbDate_click)


    Author Comment

    Oops.  On this server we're still on .net2.0alpha... Microsoft .NET Framework Version:2.0.40607.42

    LVL 6

    Accepted Solution

    I really apologise for this

    I got all confused between VB.NET and C#.NET

    This is final and correct

    AddHandler lbDate.Click, AddressOf Me.lbDate_click

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now