活动获胜';t在Web部件生命周期中开火

本文关键字:生命 周期 开火 Web 获胜 活动 | 更新日期: 2023-09-27 18:06:06

我有一个非常简单的Web部件。C点击"添加"链接,设置两个可见的文本框。键入一些文本并单击"保存"按钮,但单击事件不会触发。我正在粘贴它希望得到一些建议的代码。我一直在寻找解决方案,但没有找到任何可以继续的方法。我意识到可能是什么问题,但不知道如何纠正它。我需要能够在页面渲染之前的某个时间与处理程序进行连接和事件处理,我试图覆盖OnPreRender方法,但它没有在正确的时间工作。

我需要解决的另一个小问题是onFocus方法在txtMyLinkName.Focus((中不起作用。谢谢你的帮助!-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;                
        }
        lbMyLinkAdd.Visible = true;
        lblMyLinkName.Visible = false;
        txtMyLinkName.Visible = false;
        litBR1.Visible = false;
        lblMyLinkURL.Visible = false;
        txtMyLinkURL.Visible = false;
        btnSaveMyLink.Visible = false;
        litP1.Visible = false;
    }
}

活动获胜';t在Web部件生命周期中开火

如果您在代码中创建按钮,那么它需要在Page_Load事件中连接,以便click事件可以触发。Page_PreRender太迟了。

除了在已发布的Load事件中添加控件外,还应将ID字段(例如btnSaveMyLink.ID = "SaveLink";(设置为唯一值。