?
Solved

Dynamically create button and add code to it

Posted on 2007-10-20
5
Medium Priority
?
1,664 Views
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()
        Server.Transfer("DataCallForm.aspx")
    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?
0
Comment
Question by:wsturdev
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:answer_me
ID: 20116013
where is the code of function Go_To_Data_Call located
0
 
LVL 1

Author Comment

by:wsturdev
ID: 20116021
It is in the code behind.
0
 
LVL 1

Author Comment

by:wsturdev
ID: 20116027
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.
0
 
LVL 10

Expert Comment

by:answer_me
ID: 20116064
you should have the method call in your javascript code. and in the javascript code you'll have to write the functionality
0
 
LVL 16

Accepted Solution

by:
gnoon earned 2000 total points
ID: 20116640
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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