Adding button programatically - NON-FUNCTIONAL!!

N

Neo Geshel

Greetings. Hopefully someone will be able to untie this Gordian Knot I
have found myself bound up in.

I am trying to add a submit button dynamically to a PlaceHolder. This
button will help update a particular entry in a database.

The button is added as thus:

Sub LoadIntro()
...additional content that is dynamically loaded as well: a preview
of the DB contents followed by a form that is pre-filled from the DB...
Dim submit as Button = New Button()
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
content.Controls.Add(submit)
End Sub

And the event handler is set up like this:

Sub UpdateIntro(sender As Object, e As EventArgs)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(Request.Form("IntroComment"))
myCmd.ExecuteNonQuery()
myConn.Close()
LoadIntro()
End Sub


The problem is that this doesn't work!!! The UpdateIntro sub simply
doesn't get fired!!

I'm pulling my hair out here, as I have also removed the AddHandler to
try this:

Private Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)
Handles submit.Click

But it also throws an error! I know that adding
submit.onClick="UpdateIntro" will also throw an error, but I simply
don't know what else to do or try!

TIA
...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.†*
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationismâ€) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.†- 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
M

Mark Rae

but I simply don't know what else to do or try!

Save yourself a whole load of heartache and don't even try to add the button
dynamically - just set its Visible property to true or false as required...
 
N

Neo Geshel

Mark said:
Save yourself a whole load of heartache and don't even try to add the button
dynamically - just set its Visible property to true or false as required...

Not so easy. You see, I am trying to make an admin interface more
efficient and flexible by programatically adding content as required. If
I can solve this one single problem, I can conceivably reduce the file
size (or, in other words, the amount of code) of the admin interface by
as much as 30%.

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.†*
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationismâ€) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.†- 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
N

Newbie

I tried this out, and it works OK

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
LoadIntro()
End Sub
Sub LoadIntro()

Dim submit As Button = New Button
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
content.Controls.Add(submit)
End Sub

Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("FIRED")

End Sub


Greetings. Hopefully someone will be able to untie this Gordian Knot I
have found myself bound up in.

I am trying to add a submit button dynamically to a PlaceHolder. This
button will help update a particular entry in a database.

The button is added as thus:

Sub LoadIntro()
...additional content that is dynamically loaded as well: a preview
of the DB contents followed by a form that is pre-filled from the DB...
Dim submit as Button = New Button()
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
content.Controls.Add(submit)
End Sub

And the event handler is set up like this:

Sub UpdateIntro(sender As Object, e As EventArgs)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(Request.Form("IntroComment"))
myCmd.ExecuteNonQuery()
myConn.Close()
LoadIntro()
End Sub


The problem is that this doesn't work!!! The UpdateIntro sub simply
doesn't get fired!!

I'm pulling my hair out here, as I have also removed the AddHandler to
try this:

Private Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)
Handles submit.Click

But it also throws an error! I know that adding
submit.onClick="UpdateIntro" will also throw an error, but I simply
don't know what else to do or try!

TIA
....Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* "I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours." *
* - Stephen F. Roberts *
***********************************************************************
* "Anyone who believes in Intelligent Design ("creationism") is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* "Intelligent Design, on the other hand, has no evidence at all; not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms." - 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that "A nymphomaniac is a woman [who is] as
obsessed with sex as the average man." Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
E

Eliyahu Goldin

Are you trying to click the button on the first page load or after a
postback? You have to re-create all dynamically added controls on every
postback.

Eliyahu

Greetings. Hopefully someone will be able to untie this Gordian Knot I
have found myself bound up in.

I am trying to add a submit button dynamically to a PlaceHolder. This
button will help update a particular entry in a database.

The button is added as thus:

Sub LoadIntro()
...additional content that is dynamically loaded as well: a preview
of the DB contents followed by a form that is pre-filled from the DB...
Dim submit as Button = New Button()
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
content.Controls.Add(submit)
End Sub

And the event handler is set up like this:

Sub UpdateIntro(sender As Object, e As EventArgs)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myCmd as New OleDbCommand("UPDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(Request.Form("IntroComment"))
myCmd.ExecuteNonQuery()
myConn.Close()
LoadIntro()
End Sub


The problem is that this doesn't work!!! The UpdateIntro sub simply
doesn't get fired!!

I'm pulling my hair out here, as I have also removed the AddHandler to
try this:

Private Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)
Handles submit.Click

But it also throws an error! I know that adding
submit.onClick="UpdateIntro" will also throw an error, but I simply
don't know what else to do or try!

TIA
....Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.” *
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationism”) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all; not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.” - 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
M

Mark Rae

Not so easy. You see, I am trying to make an admin interface more efficient
and flexible by programatically adding content as required. If I can solve
this one single problem, I can conceivably reduce the file size (or, in
other words, the amount of code) of the admin interface by as much as 30%.

Hmm - I'd rethink your app design, if I were you...
 
N

Newbie

One thing you need to watch out for is complexity being built IN because you
wanted to make the interface more efficient. I find this to be a major issue
if you have too many controls to deal with. The whole thing gets to complex
to manage.

Split your functionality into logical managable units and design your UI
acordingly. Remember, the user does not know intimately how your system
works, this can lead to assumptions of ease of use by the designer which
ultimately result in user confusion.
 
M

Michael Hamrah

Dynamically loading controls can be quite a challenge. All the
controls that were added to the page dynamically most be loaded again
in the Init() event (either overriding OnInit or adding a handler to
Page.Init). The controls must be added by the Init event to the
IPostBackDataHandler and IPostBackEventHandler can work properly.
These are setup between the Init and Load event, and if the control
isn't there then it "misses the boat". The page remembers which events
need to fired during this process and then fires them after the Load
event.

If you have a series of controls you want to show/hide, group them
together into user controls (ascx files). Then, when you add them
dynamically, register a hidden field with the name of the skin file (or
files) you added. This information will be available to you when you
postback, and you can look at the values in the Init event and load the
appropriate user control in time for the page events to fire correctly.
 
N

Neo Geshel

Newbie said:
I tried this out, and it works OK

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
LoadIntro()
End Sub
Sub LoadIntro()

Dim submit As Button = New Button
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
content.Controls.Add(submit)
End Sub

Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)

Response.Write("FIRED")

End Sub

When I tried your method, I got the contents of LoadIntro() appearing
twice on the web page. When I clicked submit, I get a blank page.

What gives?

....Geshel
 
N

Neo Geshel

Neo said:
When I tried your method, I got the contents of LoadIntro() appearing
twice on the web page. When I clicked submit, I get a blank page.

What gives?

...Geshel

And when I replace your "private sub" with a normal one, LoadIntro()
appears only once on the page. But submitting the form still doesn't
update the DB, and gives me a blank page in response.

Stranger and stranger!

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.†*
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationismâ€) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.†- 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
N

Neo Geshel

Eliyahu said:
Are you trying to click the button on the first page load or after a
postback? You have to re-create all dynamically added controls on every
postback.

Eliyahu
The button is meant to be clicked on the first page load (technically).
Any button clicks should fire the UpdateIntro(), at the end of which the
LoadIntro() is called, sending the user the original form again, but
this time populated with updated material.

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.†*
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationismâ€) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.†- 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
M

Michael Hamrah

Sorry to hear my approach didn't work well for you. If controls are
being loaded twice, then that means you're calling the function twice-
could this be possible? THis can happen when you add a control in the
Init event, then add the control again after the page is loaded. Does
it occur when the page is first loaded, or only after a postback?
Secondly, as for the page not showing up, it sounds like the postback
isn't being handled correctly. THis can be related to your first
problem.

I wrote some code that adds a button to the page dynamically, stores a
value as a hidden field, then loads the button in the init event.
You'll need to wire up the Page_Init event yourself via Page.Init +=
new EventHandler(Page_Init); ANyway, you should be able to get it to
work and expand on the basic idea. You can replace the button with a
more complex usercontrol, or store a series of controls in the hidden
field if you want to add more than one:

private Button btn;
protected void Page_Init(object sender, EventArgs e)
{
string controlName;

controlName = Request.Form["ControlToAdd"];
if (controlName == "btn")
{
btn = new Button();
btn.ID = "btn";
btn.Click += new EventHandler(btn_Click);
this.form1.Controls.Add(btn);
}

}

void btn_Click(object sender, EventArgs e)
{
string s;
s = "The button has been clicked";
}

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
btn = new Button();
btn.ID = "btn";
this.form1.Controls.Add(btn);
//no need to wire up an event handler here.
this.RegisterHiddenField("ControlToAdd", "btn");
}
}

Good luck!
 
Joined
Aug 27, 2006
Messages
2
Reaction score
0
How to call Child Control button Event

Neo Geshel said:
Neo Geshel wrote:
> Newbie wrote:
>> I tried this out, and it works OK
>>
>> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles MyBase.Load
>> 'Put user code to initialize the page here
>> LoadIntro()
>> End Sub
>> Sub LoadIntro()
>>
>> Dim submit As Button = New Button
>> AddHandler submit.Click, AddressOf UpdateIntro
>> submit.id = "submit"
>> submit.Text = "Update Intro"
>> content.Controls.Add(submit)
>> End Sub
>>
>> Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)
>>
>> Response.Write("FIRED")
>>
>> End Sub

>
> When I tried your method, I got the contents of LoadIntro() appearing
> twice on the web page. When I clicked submit, I get a blank page.
>
> What gives?
>
> ...Geshel


And when I replace your "private sub" with a normal one, LoadIntro()
appears only once on the page. But submitting the form still doesn't
update the DB, and gives me a blank page in response.

Stranger and stranger!

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer *
* god than you do. When you understand why you dismiss all the other *
* possible gods, you will understand why I dismiss yours.†*
* - Stephen F. Roberts *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationismâ€) is just *
* as ignorant, irrational and ill-educated as someone who believes *
* that the world is a flat disc, that the Sun circles the Earth or *
* that there really is a tooth fairy. Darwinism has an overwhelming *
* foundation of evidence that can be tested and reproduced. *
* *
* “Intelligent Design, on the other hand, has no evidence at all;not *
* one single shred of testable proof. As such, Intelligent Design is *
* Religious Mythology, and has no right whatsoever to be in our *
* Science classrooms.†- 99.99+% of Scientists *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

loadintro()

End Sub

Sub loadintro()
Dim objButton As New Button()
objButton.ID = "DYNBUTTON1"
objButton.Text = "DYNAMIC Button"
AddHandler objButton.Click, AddressOf UpdateIntro
PlaceHolder1.Controls.Add(objButton)
End Sub

Sub UpdateIntro(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("BUTTON EVENT FIRED")
End Sub

The above code generates button at runtime. This prog works fine thru page load event. But when I copied this code to inside the BUTTON1_CLICK event it is not working.

Note:
Assume that I had created a main button called BUTTON1 by using toolbox

if I click BUTTON1 then dynamically another button say DYNBUTTON1 should generate. When i click DYNBUTTON1 then I should able to call another method
called say DISPLAYMSG() has following code:

Sub Displaymsg(ByVal sender As Object, ByVal e As EventArgs)
Response.Write("BUTTON EVENT FIRED")
End Sub

Can anybody help me to solve this problem.

Regard
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top