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

My click events are firing on linkbuttons on page load, before the link has been clicked, any ideas? (This is in a Visual web-part hosted in SP2010)

This is my code:

<asp:LinkButton ID="LinkButtonA"  PostBackUrl="<%#Request.Url.AbsoluteUri %>" runat="server" OnClick="Letter_Click" CommandArgument="A">A</asp:LinkButton>


 protected void Letter_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Label1.Text = ((LinkButton)sender).CommandArgument;
             }
        }

Label1 is having its text set to 'A' on page load.

share|improve this question
add comment

3 Answers

Tried your code and works fine: No 'A' in Label on load, and 'A' appers after click. Maybe you can give more info?

And you can try setting some labels text property value with javascript - no need to do any postback to server.

share|improve this answer
    
Thanks for spending the time trying it. Don't really have any more info to give. Did you try a Visual Webpart? The Javascript idea wont work - I need to do stuff on the server, this is just a simplified example. –  Dan Jan 12 '11 at 14:23
    
Yes i'm curently developing Visual Web Part so testing your code took few minutes and it worked fine). Try one more simple project and complete all steps. Also 1) check if one more LinkButton gives same behavior 2) Try setting AutoPostbackUrl and EventHandler on first load call. –  Andriy Shvay Jan 12 '11 at 14:54
    
And try debug it :) What you are doing for now is pure Asp. Net so you can ask Asp. Net gurus if they know the answer. –  Andriy Shvay Jan 12 '11 at 14:56
add comment

The issue seemed to be I was previewing the webpart!? When I added it to a page the events seemed to work correctly.

share|improve this answer
add comment

Are you sure the initial text of the label is empty? It might be the viewstate playing up.

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        Label1.Text = "";
    }
}

protected void Letter_Click(object sender, EventArgs e) {   
    Label1.Text = ((LinkButton)sender).CommandArgument;
}
share|improve this answer
    
I am, infact I assign a completely different value on load 'Label1.Text = "load1"' –  Dan Jan 12 '11 at 12:15
add comment

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.