Why does my sharepoint web part event handler lose the sender value on postback?

Posted by vishal shah on Stack Overflow See other posts from Stack Overflow or by vishal shah
Published on 2010-06-02T13:40:11Z Indexed on 2010/06/02 13:44 UTC
Read the original article Hit count: 638

Filed under:
|
|
|

I have a web part which is going to be a part of pair of connected web parts. For simplicity, I am just describing the consumer web part.

This web part has 10 link buttons on it. And they are rendered in the Render method instead ofCreateChildControls as this webpart will be receiving values based on input from the provider web part. Each Link Button has a text which is decided dynamically based on the input from provider web part.

When I click on any of the Link Buttons, the event handler is triggered but the text on the Link Button shows up as the one set in CreateChildControls. When I trace the code, I see that the CreateChildControls gets called before the event handler (and i think that resets my Link Buttons). How do I get the event handler to show me the dynamic text instead?

Here is the code...

public class consWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
    private bool _error = false;
    private LinkButton[] lkDocument = null;

    public consWebPart()
    {
        this.ExportMode = WebPartExportMode.All;
    }

    protected override void CreateChildControls()
    {
        if (!_error)
        {
            try
            {
                base.CreateChildControls();

                lkDocument = new LinkButton[101];
                for (int i = 0; i < 10; i++)
                {
                    lkDocument[i] = new LinkButton();
                    lkDocument[i].ID = "lkDocument" + i;
                    lkDocument[i].Text = "Initial Text";
                    lkDocument[i].Style.Add("margin", "10 10 10 10px");
                    this.Controls.Add(lkDocument[i]);
                    lkDocument[i].Click += new EventHandler(lkDocument_Click);
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<table><tr>");
        for (int i = 0; i < 10; i++)
        {
            writer.Write("<tr>");
            lkDocument[i].Text = "LinkButton" + i;
            writer.Write("<td>");
            lkDocument[i].RenderControl(writer);
            writer.Write("</td>");
            writer.Write("</tr>");
        }
        writer.Write("</table>");
    }

    protected void lkDocument_Click(object sender, EventArgs e)
    {
        string strsender = sender.ToString();
        LinkButton lk = (LinkButton)sender;
    }

    protected override void OnLoad(EventArgs e)
    {
        if (!_error)
        {
            try
            {
                base.OnLoad(e);
                this.EnsureChildControls();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }

    private void HandleException(Exception ex)
    {
        this._error = true;
        this.Controls.Clear();
        this.Controls.Add(new LiteralControl(ex.Message));
    }
}

© Stack Overflow or respective owner

Related posts about sharepoint

Related posts about web