By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

asp:button in C# class

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #2

P: n/a
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

"Marina" <so*****@nospam.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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


Nov 19 '05 #3

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

"Lam" <ja**********@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP10.phx.gbl...
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

"Marina" <so*****@nospam.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
>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
>
>



Nov 19 '05 #4

P: n/a
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
"Marina" <so*****@nospam.com> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
Do you have a form on your page in the .aspx? You need to have on there
ahead of time.

"Lam" <ja**********@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP10.phx.gbl...
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

"Marina" <so*****@nospam.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
>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
>
>



Nov 19 '05 #5

P: n/a
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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
"Marina" <so*****@nospam.com> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
Do you have a form on your page in the .aspx? You need to have on there
ahead of time.

"Lam" <ja**********@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP10.phx.gbl...
>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
>
> "Marina" <so*****@nospam.com> wrote in message
> news:OD**************@TK2MSFTNGP09.phx.gbl...
>> 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.
>>
>> "Lam" <ja**********@hotmail.com> wrote in message
>> news:%2******************@TK2MSFTNGP15.phx.gbl...
>> >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
>> >
>> >
>>
>>
>
>



Nov 19 '05 #6

P: n/a
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
----------------
Dave Fancher
http://davefancher.blogspot.com

"Lam" <ja**********@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP10.phx.gbl...
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

"Marina" <so*****@nospam.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
>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
>
>



Nov 19 '05 #7

P: n/a
Lam
Thank you guys
it's working

"Dave Fancher" <ei*****@remove.comcast.net> wrote in message
news:fI********************@comcast.com...
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
----------------
Dave Fancher
http://davefancher.blogspot.com

"Lam" <ja**********@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP10.phx.gbl...
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

"Marina" <so*****@nospam.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
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.

"Lam" <ja**********@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
>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
>
>



Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.