0

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.

3 Answers 3

1

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.

3
  • 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
    Commented Jan 12, 2011 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. Commented Jan 12, 2011 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. Commented Jan 12, 2011 at 14:56
1

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

0

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;
}
1
  • I am, infact I assign a completely different value on load 'Label1.Text = "load1"'
    – Dan
    Commented Jan 12, 2011 at 12:15

Your Answer

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

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