2013-09-29 92 views
1

我目前正在使用c#和asp.net的網站上工作。爲此,我需要創建動態控件,但是我解決了一些問題。我已經閱讀過官方文檔並且搜索了很多教程,但不幸的是,沒有人允許我解決這個問題。動態c#asp.net控件更新

這是我正在嘗試做的一個非常簡單的例子;

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
     CreateControls(); 
    else 
     UpdatePage(); 
} 

protected void CreateControls() 
{ 
    Button button1 = new Button(); 
    button1.ID = "_Button1"; 
    button1.Text = "Button1"; 
    button1.Click += new System.EventHandler(_ClickEvent); 
    _Panel.Controls.Add(button1); 

    Button button2 = new Button(); 
    button2.ID = "_Button2"; 
    button2.Text = "Button2"; 
    button2.Click += new System.EventHandler(_ClickEvent); 
    _Panel.Controls.Add(button2); 
} 

protected void UpdatePage() 
{ 
    Button button1 = ((Button)_Panel.FindControl("_Button1")); 
    button1.Text = "I went through UpdatePage and changed"; 

    Button button2 = ((Button)_Panel.FindControl("_Button2")); 
    button1.Text = "I went through UpdatePage and changed"; 
} 

protected void _ClickEvent(object sender, EventArgs e) 
{ 

} 

這裏的目的只是當點擊其中一個按鈕的文本時更改按鈕的文本。正確調用「Page_Load」方法以及「UpdatePage」方法,但在後者中,Button1和Button2控件已消失(它們不在面板控件中),並且明顯引發NullPointer異常。

會有人解釋嗎?我知道我可能錯過了關於頁面生命週期的一些內容,但無法在任何地方找到任何明確的解

非常感謝!

+3

您必須在每次回發時重新創建所有動態創建的控件。 [真正理解動態控制](http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx) –

+0

這對於一個非英語母語的人來說是一個相當困難的閱讀,但似乎真的完成了!不幸的是,我無法真正找到關於如何使用Page_Load方法來創建和更新控件的解釋。我錯過了相關段落嗎? –

回答

0

你正在創建控件第一次加載頁面時,但在Page_Load事件是來不及控件添加到頁面,並Web窗體知道這一點。

在初始頁面加載時,在OnInit和Page_Load之間的某個位置,WebForms會記錄頁面上當前的控件,並將它們設置爲視圖狀態和所有這些內容,以便下次發佈時它知道這些控件應該在那裏。如果您在Page_Load之前不添加控件,則WebForms不再真正關注您添加到頁面的內容,因此他們下次回發時不會將這些控件放在頁面上。

將您的CreateControls調用移入OnInit方法。這將告訴WebForms在適當的時候創建控件(大約與添加.aspx標記的任何控件的時間相同,儘管稍微晚些)。然後,WebForms將知道這些控件,並將應用任何必要的視圖狀態(如果它是回發),然後最終在Page_Load上,您可以使用UpdatePage調用來控制數據。

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    CreateControls(); 
} 

想到OnInit爲「將所有控件放在頁面上並連接事件處理程序」。

將Page_Load視爲「將數據放入已存在的控件中」。

0

您正在動態創建的控件在回發時將丟失。試試這個:

protected void Page_Load(object sender, EventArgs e) 
{ 
     CreateControls(); 
     UpdatePage(); 
} 

protected void CreateControls() 
{ 
    Button button1 = new Button(); 
    button1.ID = "_Button1"; 
    button1.Text = "Button1"; 
    button1.Click += new System.EventHandler(_ClickEvent); 
    _Panel.Controls.Add(button1); 

    Button button2 = new Button(); 
    button2.ID = "_Button2"; 
    button2.Text = "Button2"; 
    button2.Click += new System.EventHandler(_ClickEvent); 
    _Panel.Controls.Add(button2); 
} 

protected void UpdatePage() 
{ 
    Button button1 = ((Button)_Panel.FindControl("_Button1")); 
    button1.Text = "I went through UpdatePage and changed"; 

    Button button2 = ((Button)_Panel.FindControl("_Button2")); 
    button1.Text = "I went through UpdatePage and changed"; 
} 

protected void _ClickEvent(object sender, EventArgs e) 
{ 

} 
+0

實際上它起作用,但由於「UpdatePage」在「CreateControls」之後被調用,所以按鈕文本永遠不會是「Button1」和「Button2」,而是直接「我經歷了UpdatePage並改變了」。 我是否需要這樣做並修改事件方法「_ClickEvent」中的文本? –

0

會嘗試:

protected String TextButton1 
    { 
     get { return (String) ViewState["TextButton1"]; } 
     set { ViewState["TextButton1"] = value; } 
    } 

    protected String TextButton2 
    { 
     get { return (String)ViewState["TextButton2"]; } 
     set { ViewState["TextButton2"] = value; } 
    } 

    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     CreateControls(); 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      UpdatePage(); 
     } 
    } 

    protected void CreateControls() 
    { 
     Button button1 = new Button(); 
     button1.ID = "_Button1"; 
     button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1; 
     button1.Click += new System.EventHandler(_ClickEvent1); 
     _Panel.Controls.Add(button1); 

     Button button2 = new Button(); 
     button2.ID = "_Button2"; 
     button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2; 
     button2.Click += new System.EventHandler(_ClickEvent2); 
     _Panel.Controls.Add(button2); 
    } 

    protected void UpdatePage() 
    { 
     Button button1 = ((Button)_Panel.FindControl("_Button1")); 
     button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1; 

     Button button2 = ((Button)_Panel.FindControl("_Button2")); 
     button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2; 
    } 

    protected void _ClickEvent1(object sender, EventArgs e) 
    { 
     TextButton1 = "test"; 
     Button b = (Button) sender ; 
     b.Text = TextButton1; 
    } 

    protected void _ClickEvent2(object sender, EventArgs e) 
    { 
     TextButton2 = "test"; 
     Button b = (Button)sender; 
     b.Text = TextButton2; 
    }