Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Im having issues with the onClick event on LinkButtons (possibly all controls) not working, in a Visual Webpart I am writing for SP2010.

Simple example of what i am doing:

public partial class MyUserControl : UserControl
{
   protected void Page_Load(object sender, EventArgs e)
   {
       Label1.Text = "load";
   }

  protected void LinkButton1_Click(object sender, EventArgs e)
  {
      Label1.Text = "click";
  }
}

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

<asp:Label ID="Label1" runat="server"></asp:Label>

Webpart loads fine with Label1's text being set to 'load', but clicking on the button has no effect.

An ideas?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Add AutoPostBack="true" to LinkButton and it will work fine.

<asp:LinkButton ID="LinkButton1" runat="server" AutoPostBack="true" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>


[Update]

Sorry for the missleading answer. Checked out the LinkButton - no AutoPostBack property, but there is PostBackUrl property - assign it the url of page that contains your web part and it will work - tested. For other controls like Buttons, DropDownList it's AutoPostBack to use.

share|improve this answer
    
LinkButton does not have the AutoPostBack attribute? –  Dan Jan 11 '11 at 17:43
    
Dan, I have updated my answer. –  Andriy Shvay Jan 12 '11 at 9:03
    
Is there anyway to automatically determine this so that there is no hard coding. –  Dan Jan 12 '11 at 9:11
    
In your code behind you can use Request.Url.AbsoluteUri –  Andriy Shvay Jan 12 '11 at 9:27
    
If it helped you do not forget to mark as answer) –  Andriy Shvay Jan 12 '11 at 12:48

Couldn't reproduce this problem. Are you sure the controls are inside a form? This can happen if you have a mishap when editing the master page. Another possibility is if you change the attributes on the page or in the control, e.g. AutoEventWireup.

share|improve this answer
    
A Visual Webpart is a user control so its kinda hard to put the controls out of the form as there are no form tags. AutoEventWireup is set to true. –  Dan Jan 11 '11 at 17:48
    
There will be a form tag in the rendered page - it will probably originate from the master page for the site page in which the webpart is rendered. You can check the rendered HTML. –  SPDoctor Jan 11 '11 at 21:28

Add it in the CreateChildControls() event in the webpart, like:

protected override void CreateChildControls()
{      
    LinkButton linkbutton = new LinkButton();
    linkbutton.Click += ClickButton;
    this.Controls.Add(linkbutton);

}

protected void ClickButton(object sender, EventArgs e)
{
    // handle event
}
share|improve this answer
1  
This is a 2010 Visual Webpart, doesn't this defy the point of using a visual webpart, doing it this way the control wont appear in the designer\source view. –  Dan Jan 11 '11 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.