473,856 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Control s.Add(submit)
End Sub

And the event handler is set up like this:

Sub UpdateIntro(sen der As Object, e As EventArgs)
Dim myConn as New
OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
Dim myCmd as New OleDbCommand("U PDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandTy pe = CommandType.Tex t
myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
RepChar(Request .Form("IntroCom ment"))
myCmd.ExecuteNo nQuery()
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(ByV al sender As Object, ByVal e As EventArgs)
Handles submit.Click

But it also throws an error! I know that adding
submit.onClick= "UpdateIntr o" 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
11 1608
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(Pa ge_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(objec t sender, EventArgs e)
{
string controlName;

controlName = Request.Form["ControlToA dd"];
if (controlName == "btn")
{
btn = new Button();
btn.ID = "btn";
btn.Click += new EventHandler(bt n_Click);
this.form1.Cont rols.Add(btn);
}

}

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

protected void Page_Load(objec t sender, EventArgs e)
{
if (Page.IsPostBac k == false)
{
btn = new Button();
btn.ID = "btn";
this.form1.Cont rols.Add(btn);
//no need to wire up an event handler here.
this.RegisterHi ddenField("Cont rolToAdd", "btn");
}
}

Good luck!

Mar 9 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1032
by: Neo Geshel | last post by:
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
9
2797
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT IS IMPOSSIBLE TO PROGRAMMATICALLY ADD A BUTTON TO A DYNAMICALLY CREATED PAGE. Or, to be more precise, why it is impossible to have an onClick sub respond to that button’s Click event. My main page has only one line:
0
9920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11063
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10699
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10790
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4580
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4176
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.