asp:button in C# class

L

Lam

I want to dynamic generate a asp:button in C# class, not in the HTML code
so that it can call the methods in the c# class,
I try to use "Response.Write("<asp:button...>")
it didn't show the button
Can anyone help?

Thanks a lot
 
M

Marina

This was asked a couple of days ago, and many many times before then.

The end result being, is that those tags are interpreted by asp.net, and
then your code runs. You can't then write out more of those tags, and want
them to be interpreted by the engine.

Forget the way you did everything in ASP. Pretend you never did ASP at all.
All it will every do is cause you to not do things the right way.

Then read up on ASP.NET and the object oriented model before going any
further.

You will see that the right way to do this is to create actual objects and
add them to the page.
 
L

Lam

I have created a method like this
private void createSaveButton()

{

System.Web.UI.WebControls.Button b1= new
System.Web.UI.WebControls.Button();

b1.Text="Save it";

Page.Controls.Add(b1);


}

when I run the code, it says that the button has to be added in the form

how can I do that?

thanks
 
M

Marina

Do you have a form on your page in the .aspx? You need to have on there
ahead of time.
 
L

Lam

thanks
one last question
how can I write methods to handle the event when the button is clicked?
is it b1.click? this is the event handler

thanks for your time
 
M

Marina

You have to write the handler, meaning it has to have the right signature.
Then, you would do something like:

AddHandler myBtn.Click, AddressOf MyClickHandler

where MyClickHandler is the method you wrote.
 
D

Dave Fancher

From where is this method called? There are some considerations that must
be made when dynamically adding controls to a page in order for them to
function properly.

As for dynamically setting the event handler, you will need to write the
event handler using the correct signature:
protected void myButton_OnClick(Object sender, EventArgs e)
...

protected void myButton_OnCommand(Object sender, CommandEventArgs e)
...

depending on what you're trying to do. You would assign the handler to the
button in the following manner:
myButton.OnClick += new EventHandler(myButton_OnClick)
or
myButton.OnCommand += new CommandEventHandler(myButton_OnCommand)

HTH
 
L

Lam

Thank you guys
it's working

Dave Fancher said:
From where is this method called? There are some considerations that must
be made when dynamically adding controls to a page in order for them to
function properly.

As for dynamically setting the event handler, you will need to write the
event handler using the correct signature:
protected void myButton_OnClick(Object sender, EventArgs e)
...

protected void myButton_OnCommand(Object sender, CommandEventArgs e)
...

depending on what you're trying to do. You would assign the handler to the
button in the following manner:
myButton.OnClick += new EventHandler(myButton_OnClick)
or
myButton.OnCommand += new CommandEventHandler(myButton_OnCommand)

HTH
 

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,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top