471,602 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

Adding button programatically - NON-FUNCTIONAL!!

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.
************************************************** *********************
Mar 8 '06 #1
11 1462
"Neo Geshel" <go****@geshel.org> wrote in message
news:IuzPf.122899$sa3.82272@pd7tw1no...
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...
Mar 8 '06 #2
Mark Rae wrote:
"Neo Geshel" <go****@geshel.org> wrote in message
news:IuzPf.122899$sa3.82272@pd7tw1no...
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...


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.
************************************************** *********************
Mar 8 '06 #3
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
"Neo Geshel" <go****@geshel.org> wrote in message
news:IuzPf.122899$sa3.82272@pd7tw1no...
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.
************************************************** *********************
Mar 8 '06 #4
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

"Neo Geshel" <go****@geshel.org> wrote in message
news:IuzPf.122899$sa3.82272@pd7tw1no...
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.
************************************************** *********************
Mar 8 '06 #5
"Neo Geshel" <go****@geshel.org> wrote in message
news:l%zPf.124974$B94.55874@pd7tw3no...
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...
Mar 8 '06 #6
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.
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Neo Geshel" <go****@geshel.org> wrote in message
news:l%zPf.124974$B94.55874@pd7tw3no...
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...

Mar 8 '06 #7
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.

Mar 8 '06 #8
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
Mar 9 '06 #9
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.
************************************************** *********************
Mar 9 '06 #10
Eliyahu Goldin wrote:
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.
************************************************** *********************
Mar 9 '06 #11
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!

Mar 9 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.