Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Developers,

Am working on a sharepoint webpart project,

I have created one label using below code

Label lbltest = new Label ();

Controls.add(lbltest);

Now i want to write a click event for this label , how can i do that?
There is no asp design page only c # file since it is sharepoint project.
Posted
Comments
ravikhoda 27-Feb-14 1:34am    
try and add event handler for lable click event from code.
Am Gayathri 27-Feb-14 1:42am    
Thanks for you replay,

But could you please tell me how can i do that?
Ramug10 27-Feb-14 1:36am    
Hi Kavitha,

Asp.Net Label control doesn't have click event so you can use linkbutton instead of label.
Am Gayathri 27-Feb-14 1:41am    
Thanks for you replay,
Even am ready to use button , but i dont know how to write that clcik event.
Usually what i do is i will create button in asp page and double click on that button will take me to click event. But here am adding button using c# code
Button btn = new button();
so how can i write event for this?
rezaasaadi 27-Feb-14 2:03am    
// this can be put in form/page load or form constructor
maybe this is a solution:
btn.Click += new EventHandler(MyClick);

// and this function should be added as a member function of form or page
private void MyClick(object sender, EventArgs e)
{
}

yeah Ramug10 is right you can not have click event of lable. if you are using button you can do something like below.
C#
btnPrint.Click += new EventHandler(btnPrint_Click);



void btnPrint_Click(object sender, EventArgs e)
       {
           your logic goes here
       }
 
Share this answer
 
Comments
Am Gayathri 27-Feb-14 2:03am    
Can i use this above logic for System.Web.UI.WebControls button ? I guess this is for System.Windows.Forms button . But mine is webapplication.
ravikhoda 27-Feb-14 2:22am    
yeah sure you can use this one on web controls as well.
Am Gayathri 27-Feb-14 5:32am    
Yes it is wroking Thanks :) Now i want to konw that can i add the click event like this for a tree view node?
ravikhoda 27-Feb-14 5:38am    
i guess it should work. not personally tried.
Use below code..

In Page_Load
Button btnnew = new Button();
            btnnew.ID = "btnSubmit";
            form1.Controls.Add(btnnew);
            btnnew.Click += btnnew_Click;


add this Event handler method outside the load
C#
void btnnew_Click(object sender, EventArgs e)
       {
          //Add your logic  when this button click fired
       }
 
Share this answer
 
Comments
Am Gayathri 27-Feb-14 2:03am    
Can i use this above logic for System.Web.UI.WebControls button ?
I guess this is for System.Windows.Forms button . But mine is webapplication.
Ramug10 27-Feb-14 2:09am    
yes, you can use this for webapplication.
Am Gayathri 27-Feb-14 2:25am    
How?
button test = new button().

I dont see any property like .Click for this button.(This is a stem.Web.UI.WebControls button not system.forms.button)
Ramug10 27-Feb-14 2:51am    
you should get event for your button 'test' like test.Click +=test_Click;
Am Gayathri 27-Feb-14 5:32am    
Yes it is wroking
Thanks :)
Now i want to konw that can i add the click event like this for a tree view node?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900