2009-05-11 11 views
1

linkbuttonのenableviewstateプロパティとlabelをtrueに設定しています.pageloadイベントハンドラのポストバックで同じボタンを再生成します.button.butのlinkclockイベントハンドラを呼び出せませんでした。コードに問題がありますか教えてください。Asp.net:to動的に生成されるボタンのイベントハンドラを呼び出しますか?

公共部分クラス_Default:System.Web.UI.Page { 保護のボイドをPage_Load(オブジェクト送信者、EventArgsの電子) {

 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; 
     } 
    } 
} 

}

答えて

1

あなたはどのような側面についての仮定を作っていますそのボタンのセッションはセッションに保存されます。ページの読み込み中にセッション状態から復元するときに、ボタンにイベントを再度追加してみてください。

0

イベントハンドラをセッションから取得した後にもう一度ボタンに割り当てて解決策を得ましたが、保存済みのセッション変数に既に割り当てられているため、再度割り当てる必要がある理由を理解できませんでした。

+1

イベントハンドラをシリアライズすることが何を意味するのかを考えると、基本的には関数へのポインタをシリアライズすることになりますが、それはおそらく非常に有益な機能ではないことが明らかになります。 –

関連する問題