0

I am implementing a website using ASP.NEt 2.0 and I want to add a new Label when I press an existing button Could some body help me on doing this ????

4
  • Are you intending to do this without a Postback? If so, you'll need ASP.NET AJAX (or another similar library) to accomplis this. Commented Apr 17, 2009 at 16:08
  • duplicate of (for example): stackoverflow.com/questions/737981/… Commented Apr 17, 2009 at 16:11
  • @Martin: That was about a Textbox! This is about a Label! It's a whole world of difference, man! :P Commented Apr 17, 2009 at 16:20
  • 1
    @Cerebrus - you're right about that, adding a textbox might actually be useful! Commented Apr 17, 2009 at 16:24

6 Answers 6

2

in the OnClick event for the button:

Label lbl = new Label();
lbl.Text = "some text";

ControlContainingLabel.Controls.Add(lbl);
Sign up to request clarification or add additional context in comments.

Comments

2

You can create a label using Syed Tayyab Ali's answer, however if someone clicks another button and you get another postback, your first created label will disappear as the page will be recreated from scratch.

Comments

1

If the button doesn't depend on what is going in the text of the label, or the server is not updating with info too display in label then there is no reason to add it on the server side. you can easily put the label where you want in your client side code and use some form of javascript (jQuery) to hide it and when the button is clicked then unhide the label as so:

   $(function(){
    $('.label1').hide(); //hide the label with the class name label1
    $('.button1').click(function(){  // bind the button's click event
      $('.label1').show(); //unhide the label 

    });
   });

Comments

0

you need to create new instance of lable then add your label control by using control.add(lable1) in pre-existing button event.

it will add your label on page, whenever you hit pre-existing button.

Comments

0

here a simple solution that might help u . first add a label to the container( eg form, panel, etc) and hide it using the following code:-

label1.hide();

you have to enter the above code in the form_load function then in the button1_click function enter the below code

label1.show();
label1.text="some text";

Comments

-1

Label lbl = new Label(); lbl.Text = "some text";

ControlContainingLabel.Controls.Add(lbl);

จากโค้ดนี้ค่ะ อยากจะกำหนดตำแหน่งที่จะให้ label lbl แสดงค่ะ ต้องเขียนโค้ดอะไรเพิ่มคะ

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.