开发者

Event won't fire in webpart lifecycle

开发者 https://www.devze.com 2023-03-29 15:52 出处:网络
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 hop

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.

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

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("<br />"));

        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("&nbsp;&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("<br />"));

            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("<table class='mylinksTable' cellpadding='0' cellspacing='0' border='1'><tr valign='top'><td>"));
        pnlMyLinks.Controls.Add(lblMyLinkName); 
        pnlMyLinks.Controls.Add(new LiteralControl("</td><td>"));
        pnlMyLinks.Controls.Add(txtMyLinkName);
        pnlMyLinks.Controls.Add(new LiteralControl("</td></tr><tr valign='top'><td>"));
        pnlMyLinks.Controls.Add(lblMyLinkURL);
        pnlMyLinks.Controls.Add(new LiteralControl("</td><td>"));
        pnlMyLinks.Controls.Add(txtMyLinkURL);
        pnlMyLinks.Controls.Add(new LiteralControl("</td></tr><tr valign='top'><td colspan='2'>"));
        pnlMyLinks.Controls.Add(btnSaveMyLink);
        pnlMyLinks.Controls.Add(new LiteralControl("</td></tr></table>"));

        this.Controls.Add(pnlMyLinks);
    }

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

        if ((txtMyLinkName.Text != "") && (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;   开发者_如何学Python             
        }

        lbMyLinkAdd.Visible = true;
        lblMyLinkName.Visible = false;
        txtMyLinkName.Visible = false;
        litBR1.Visible = false;
        lblMyLinkURL.Visible = false;
        txtMyLinkURL.Visible = false;
        btnSaveMyLink.Visible = false;
        litP1.Visible = false;
    }
}


If you are creating the button in code, then it needs to be wired up in the Page_Load event so that the click event can fire. Page_PreRender is too late.


In addition to adding the control in Load event as already posted, you should set the ID field e.g. btnSaveMyLink.ID = "SaveLink"; to a unique value.

0

精彩评论

暂无评论...
验证码 换一张
取 消