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

V

vladvadeanu

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:
 
A

avnrao

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.
 
V

vladvadeanu

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 :)
 
H

Hans Kesting

vladvadeanu said:
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
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top