Event won't fire in webpart lifecycle
<p>I have a webpart that is quire simple. CLick on "Add" link that sets a 2 text boxes visible. Type in some text and click "Save" button but the click event won't fire. I'm pasting the code it hopes of some suggestion. I've searched for solutions but haven't found anything I can go on. I realize what maight be the issue but don't know how to corrct it. I need to be able to wireup and event with the handler sotimes before the page renders and I tried to override the OnPreRender method but it did not work in right moment.</p>

<p>Other minor issue that I will need to address is that the onFocus method doesn't work in txtMyLinkName.Focus(). Thanks for your help! - Risho</p>

<pre><code>public class MyLinks : WebPart
{
    public static string m_Portal = ConfigurationManager.ConnectionStrings["dbPortal"].ConnectionString;

    Panel pnlMyLinks = new Panel();
    Label lblError = new Label();
    Label lblMyLinkURL = new Label();
    Label lblMyLinkName = new Label();
    TextBox txtMyLinkName = new TextBox();
    TextBox txtMyLinkURL = new TextBox();
    Button btnSaveMyLink = new Button();                    
    LinkButton lbMyLinkAdd = new LinkButton();
    Literal litP1 = new Literal();
    Literal litBR1 = new Literal();

    public cisf_MyLinks()
    {
        this.Title = "MyLinks";
        this.ExportMode = WebPartExportMode.All;
    }

    protected override void CreateChildControls()
    {
        GetLinks();
        base.CreateChildControls();
    }

    //protected override void OnPreRender(EventArgs e)
    //{
    //    btnSaveMyLink.Text = "Save";
    //    btnSaveMyLink.Click += new EventHandler(btnSaveMyLink_Click);
    //    Controls.Add(btnSaveMyLink);
    //    base.OnPreRender(e);
    //}

    protected void GetLinks()
    {
        pnlMyLinks.Controls.Clear();
        int i = 0;            
        lbMyLinkAdd.Text = "Add";
        pnlMyLinks.Controls.Add(lbMyLinkAdd);
        lbMyLinkAdd.Click += new EventHandler(lbMyLinkAdd_Click);
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;br /&gt;"));

        IDataReader drMyLinks = Get_MyLinks(Page.Request.ServerVariables["Logon_User"].Split("\\".ToCharArray())[1].ToLower());
        while (drMyLinks.Read())
        {
            HyperLink hlMyLink = new HyperLink();
            LinkButton lbDelMyLink = new LinkButton();
            lbDelMyLink.Text = "(del)";
            lbDelMyLink.ToolTip = "Delete this link";
            lbDelMyLink.CssClass = "verytiny";
            lbDelMyLink.Command += new CommandEventHandler(DelMyLink);
            lbDelMyLink.CommandName = drMyLinks["id"].ToString();
            pnlMyLinks.Controls.Add(lbDelMyLink);
            pnlMyLinks.Controls.Add(new LiteralControl("&amp;nbsp;&amp;nbsp;"));


            hlMyLink.ID = "hl" + drMyLinks["ID"].ToString();
            hlMyLink.Text = drMyLinks["Title"].ToString();
            hlMyLink.NavigateUrl = drMyLinks["url"].ToString();
            hlMyLink.Target = "_blank";
            hlMyLink.ToolTip = drMyLinks["Title"].ToString();
            pnlMyLinks.Controls.Add(hlMyLink);
            pnlMyLinks.Controls.Add(new LiteralControl("&lt;br /&gt;"));

            if (drMyLinks["ID"].ToString() != "") { i += 1; }
        }
        this.Controls.Add(pnlMyLinks);
    }

    protected void lbMyLinkAdd_Click(object sender, EventArgs e)
    {
        lbMyLinkAdd.Visible = false;
        lblMyLinkName.Visible = true;
        txtMyLinkName.Visible = true;

        litBR1.Visible = true;
        lblMyLinkURL.Visible = true;
        txtMyLinkURL.Visible = true;
        btnSaveMyLink.Visible = true;
        litP1.Visible = true;
        (txtMyLinkName - dot focus)
        lblMyLinkName.Text = "Link Name: ";
        lblMyLinkURL.Text = "Link URL: ";

        btnSaveMyLink.Text = "Save";
        btnSaveMyLink.Click += new EventHandler(btnSaveMyLink_Click);

        pnlMyLinks.Controls.Add(new LiteralControl("&lt;table class='mylinksTable' cellpadding='0' cellspacing='0' border='1'&gt;&lt;tr valign='top'&gt;&lt;td&gt;"));
        pnlMyLinks.Controls.Add(lblMyLinkName); 
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;td&gt;"));
        pnlMyLinks.Controls.Add(txtMyLinkName);
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;/tr&gt;&lt;tr valign='top'&gt;&lt;td&gt;"));
        pnlMyLinks.Controls.Add(lblMyLinkURL);
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;td&gt;"));
        pnlMyLinks.Controls.Add(txtMyLinkURL);
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;/tr&gt;&lt;tr valign='top'&gt;&lt;td colspan='2'&gt;"));
        pnlMyLinks.Controls.Add(btnSaveMyLink);
        pnlMyLinks.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;"));

        this.Controls.Add(pnlMyLinks);
    }

    protected void btnSaveMyLink_Click(object sender, EventArgs e)
    {
        string thisURL;

        if ((txtMyLinkName.Text != "") &amp;&amp; (txtMyLinkURL.Text != ""))
        {
            if (txtMyLinkURL.Text.StartsWith("http"))
            { thisURL = txtMyLinkURL.Text; }
            else { thisURL = "http://" + txtMyLinkURL.Text; }

            AddMyLink(txtMyLinkName.Text, thisURL, Page.Request.ServerVariables["Logon_User"].Split("\\".ToCharArray())[1].ToLower());
            GetLinks();
            txtMyLinkName.Text = "";
            txtMyLinkURL.Text = "";
            lbMyLinkAdd.Visible = true;                
        }

        lbMyLinkAdd.Visible = true;
        lblMyLinkName.Visible = false;
        txtMyLinkName.Visible = false;
        litBR1.Visible = false;
        lblMyLinkURL.Visible = false;
        txtMyLinkURL.Visible = false;
        btnSaveMyLink.Visible = false;
        litP1.Visible = false;
    }
}
</code></pre>
