Dynamically create button and add code to it

Posted on 2007-10-20
Last Modified: 2012-08-13
I am able to dynamically create a button.  What I cannot figure out is how to add code to it.

I have tried creating a simple handler:
    Protected Sub Go_To_Data_Call()
    End Sub
and then when I create the button, assigning an OnClick attribute:
                    btnGoToDataCall.Attributes.Add("OnClick", "Go_To_Data_Call();")
This gives an error "Microsoft JScript runtime error: object expected"

Then, I have tried to eliminate the handler and just add the OnClick attribute that executes the server transfer directly:
                   btnGoToDataCall.Attributes.Add("OnClick", "Server.Transfer('DataCallForm.aspx');")
This gives an error "Microsoft JScript runtime error: 'Server' is undefined"

What am I doing wrong?
Question by:wsturdev
    LVL 10

    Expert Comment

    where is the code of function Go_To_Data_Call located
    LVL 1

    Author Comment

    It is in the code behind.
    LVL 1

    Author Comment

    More precisely, it is in the code behind for the page that the button is being created on -- the same place where the code is that adds the attribute.
    LVL 10

    Expert Comment

    you should have the method call in your javascript code. and in the javascript code you'll have to write the functionality
    LVL 16

    Accepted Solution

    Do no add attribute btnGoToDataCall.Attributes.Add("OnClick", "Go_To_Data_Call();").
    It does not invoke to the function in code behind, but a javascript function on page.
    Since you did not implement the function, runtime error: object expected is thrown.

    You must create the button with the same ID and add event handler evertime the page request, to make it exist for every postback. Typically, just create it in page_init()

    Dim objButton As New Button
    Page.FindControl("Form1").Controls.Add(objButton) '<--- Add it to page before set properties for safety
    objButton.Text = "Click me!"
    objButton.ID = "ButtonID"
    AddHandler objButton.Click, AddressOf Go_To_Data_Call

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now