【问题标题】:Asp.net:to call event handler of dynamically generated buttons?Asp.net:调用动态生成按钮的事件处理程序?
【发布时间】:2010-10-25 05:22:34
【问题描述】:

我已将链接按钮和标签的 enableviewstate 属性设置为 true。还在页面加载事件处理程序的回发中重新生成相同的按钮。但我无法调用链接按钮的 onclick 事件处理程序。你能告诉我代码有什么问题吗?

公共部分类_默认:System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

        List<LinkButton> listOfLinkButton = Session["ListOfLinkButton"] as List<LinkButton>;
        List<Label> listOfLabel = Session["ListOfLabel"] as List<Label>;
        if(listOfLabel!=null && listOfLinkButton!=null)
        {
            for (int i = 0; i < listOfLinkButton.Count; i++)
            {
                PlaceHolder1.Controls.Add(listOfLinkButton[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
                PlaceHolder1.Controls.Add(listOfLabel[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
            }
        }



}



protected void LinkButton_Click(object sender, EventArgs e)
{
    LinkButton linkButton = sender as LinkButton;

    Response.Redirect(linkButton.Attributes["LinkUrl"]);        
}
protected void Button1_Click1(object sender, EventArgs e)
{

    List<LinkButton> listOfLinkButton = new List<LinkButton>();
    List<Label> listOfLabel = new List<Label>();
    Rss rssDocumentObj = RssFileReader.GetRssDocumentData(TextBox1.Text);
    for (int j = 0; j < rssDocumentObj.ListOfChannel.Count; j++)
    {
        LinkButton linkButton = new LinkButton();
        linkButton.ID = "LinkButtonForChannelDynamicInPlaceHolder1Id" + j;
        linkButton.EnableViewState = true;
        linkButton.ForeColor = Color.Blue;
        linkButton.Font.Bold = true;
        linkButton.Font.Size = 18;
        linkButton.Font.Underline = true;
        linkButton.Text = rssDocumentObj.ListOfChannel[j].ChannelTitle.InnerText;
        linkButton.Click += new EventHandler(LinkButton_Click);
        linkButton.Attributes.Add("LinkUrl", rssDocumentObj.ListOfChannel[j].ChannelLink.InnerText);
        linkButton.Attributes.Add("onmouseover", "this.style.color = '#006699'");
        linkButton.Attributes.Add("onmouseout", "this.style.color = '#0000ff'");
        PlaceHolder1.Controls.Add(linkButton);
        listOfLinkButton.Add(linkButton);
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        Label label = new Label();
        label.ID = "LabelForChannelDynamicInPlaceHolder1Id" + j;
        label.EnableViewState = true;
        label.ForeColor = Color.DarkSlateGray;
        label.Text = rssDocumentObj.ListOfChannel[j].ChannelDescription.InnerText;
        PlaceHolder1.Controls.Add(label);
        listOfLabel.Add(label);
        PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

        for (int i = 0; i < rssDocumentObj.ListOfChannel[j].ListOfItem.Count; i++)
        {
            LinkButton linkButtonForItem = new LinkButton();
            linkButtonForItem.ID = "LinkButtonDynamicInPlaceHolder1Id" + j + " " + i;
            linkButtonForItem.EnableViewState = true;
            linkButtonForItem.ForeColor = Color.Blue;
            linkButtonForItem.Font.Bold = true;
            linkButtonForItem.Font.Size = 14;
            linkButtonForItem.Font.Underline = false;
            linkButtonForItem.Text = rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemTitle.InnerText;
            linkButtonForItem.Click += new EventHandler(LinkButton_Click);
            linkButtonForItem.Attributes.Add("LinkUrl", rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemLink.InnerText);
            linkButtonForItem.Attributes.Add("onmouseover", "this.style.color = '#006699'");
            linkButtonForItem.Attributes.Add("onmouseout", "this.style.color = '#0000ff'");
            PlaceHolder1.Controls.Add(linkButtonForItem);
            listOfLinkButton.Add(linkButtonForItem);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
            Label labelForItem = new Label();
            labelForItem.ID = "LabelDynamicInPlaceHolder1Id" + i;
            labelForItem.EnableViewState = true;
            labelForItem.ForeColor = Color.DarkGray;
            labelForItem.Text = rssDocumentObj.ListOfChannel[j].ListOfItem[i].ItemDescription.InnerText;
            PlaceHolder1.Controls.Add(labelForItem);
            listOfLabel.Add(labelForItem);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
            Session["ListOfLinkButton"] = listOfLinkButton;
            Session["ListOfLabel"] = listOfLabel;
        }
    }  
}

}

【问题讨论】:

    标签: asp.net dynamic button call event-handling


    【解决方案1】:

    您正在假设该按钮的哪些方面保存在会话中。当您在页面加载期间从会话状态恢复时,请尝试再次将事件添加到按钮。

    【讨论】:

      【解决方案2】:

      我通过在从会话中检索到按钮后再次将事件处理程序分配给按钮来得到我的解决方案,但是当它已经分配到存储的会话变量中时,我不明白为什么我需要再次分配它。

      【讨论】:

      • 如果您考虑序列化事件处理程序意味着什么,即您基本上是在序列化指向函数的指针,很明显这可能不是一个非常有益的功能,IMO .
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      相关资源
      最近更新 更多