468,133 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

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.Add(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 1925
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.Attributes.Add("onclick","myfunction();")

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

Av.

"vladvadeanu" <vl**********@yahoo-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.Add(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.Add(myButton);
myButton.Attributes.Add("onclick","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

"vladvadeanu" <vl**********@yahoo-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.Add(myButton);
myButton.Attributes.Add("onclick","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(Generic_Button_Click);
myBtn.ID = "whatever";

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


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.