473,581 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I dinamically add controls and events to a Web Form

I have this problem...
I want to add controls to a WebForm (any kind of controls... panels,
buttons, datagrids,...) at runtime and I could do that with
HtmlControls :" Label myLabel = new Label();
this.Controls.A dd(myLabel) " but I don't know how to add events to
them such as onClick, OnKeyDown... There is a ServerClick event that
doesn't seem to be working...
If anyone could help me :wink:

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #1
3 2029
do you want to add HTML controls or Server controls dynamically?

you should create them in Page_Load (do not put them in !IsPostBack)
and for events add
button.Attribut es.Add("onclick ","myfunction() ;")

ServerClick works on the server as if the button is server control. it
worked for me.

Av.

"vladvadean u" <vl**********@y ahoo-dot-com.no-spam.invalid> wrote in message
news:40******** @127.0.0.1...
I have this problem...
I want to add controls to a WebForm (any kind of controls... panels,
buttons, datagrids,...) at runtime and I could do that with
HtmlControls :" Label myLabel = new Label();
this.Controls.A dd(myLabel) " but I don't know how to add events to
them such as onClick, OnKeyDown... There is a ServerClick event that
doesn't seem to be working...
If anyone could help me :wink:

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---

Nov 18 '05 #2
The thing is that I want to dinamicaly create a web page from scratch
: to add controls and events to them from a database containing
properties and events. So I tried to add controls at runtime and the
only controls I could add were HtmlControls... But with the events is
a bit difficult because I want to do a postback to the server telling
it I pushed that button and I want to add another label or whatever
according to the button's event, and the server to send me the new
page :)

I tried it this way but it doesn't work... it's all written in the
code-behind file where I want to put most of my code if possible:
void myfunc()
{
aLabel.Text="it worked!"; //something to tell me it
worked
}

void aButton_click(. .)
{
HtmlButton myButton=new HtmlButton();
this.Controls.A dd(myButton);
myButton.Attrib utes.Add("oncli ck","myfunc();" );
}

Maybe I didn't understand correctly the thing with HtmlControls...

Thanks for your help :)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #3

"vladvadean u" <vl**********@y ahoo-dot-com.no-spam.invalid> wrote in message news:40******** **@127.0.0.1...
The thing is that I want to dinamicaly create a web page from scratch
: to add controls and events to them from a database containing
properties and events. So I tried to add controls at runtime and the
only controls I could add were HtmlControls... But with the events is
a bit difficult because I want to do a postback to the server telling
it I pushed that button and I want to add another label or whatever
according to the button's event, and the server to send me the new
page :)

I tried it this way but it doesn't work... it's all written in the
code-behind file where I want to put most of my code if possible:
void myfunc()
{
aLabel.Text="it worked!"; //something to tell me it
worked
}

void aButton_click(. .)
{
HtmlButton myButton=new HtmlButton();
this.Controls.A dd(myButton);
myButton.Attrib utes.Add("oncli ck","myfunc();" );
}

Maybe I didn't understand correctly the thing with HtmlControls...

Thanks for your help :)


As you found out, this will not work! the "onclick" attribute is
a client-side function. It is not possible to call a server-side
function this way!

What you can do:
write a generic eventhandler. When you add a button to the page,
have it's Click-event handler point to yout generic handler.
In that handler you can check the source to see what button exactly
was clicked.

something like this:

Button myBtn = new Button();
myBtn.Click += new Eventhandler(Ge neric_Button_Cl ick);
myBtn.ID = "whatever";

private void Generic_Button_ Click(object sender, System.EventArg s e)
{
// your handler code ..
Button btn = sender as Button;
if (btn != null)
{
if (btn.ID == "whatever")
{
// etc...
}
}
}
Hans Kesting


Nov 18 '05 #4

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

Similar topics

2
1256
by: Umut Tezduyar | last post by:
public class WebForm1 : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { }
0
968
by: Rafi | last post by:
Hi, In my database I have table category where I store diffrent categories and subcategories. I decied to create LinkButtons dinamically with assigned name of category and its id. I also implemented an event onClick which task is to create LinkButton subcategories. When the page is loaded main categories are displayed, when I click on any...
22
2160
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said stage. One approach I have used on other systems is to prevent the action buttons appearing. For example, if one did not have the Role of...
2
1738
by: basetta | last post by:
Hi, I need to know how I can use some objects create dinamically in vb.net form. My code creates some buttons in my windows form, then I would like to manage their click events, but I don't know how I can do it. can you help me? thanks
0
7862
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8144
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. ...
0
8301
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...
1
7894
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...
0
8169
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6551
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3803
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1400
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.