Asp.net
回發後動態列消失
我有一個
GridViewwith someBoundFields和 twoTemplateFields。在這兩個TemplateFields中,我動態創建UserControls包含 aDropDownList和 aTextBox,使用者可以修改它們。當我嘗試在 a 之後獲取控制項的
PostBack值時,其中的值BoundFields仍然存在,但我的動態控制項消失了。我可以再次創建它們,但它不會獲得使用者的值……我怎樣才能在它們失去之前獲得這些值?這是我的一些程式碼:
在
RowDataBound事件中:Select Case type Case "BooleanBis" e.Row.Cells(2).Controls.Clear() Dim list1 As BooleanBisList = New BooleanBisList(avant, False) e.Row.Cells(2).Controls.Add(list1) e.Row.Cells(4).Controls.Clear() Dim list2 As BooleanBisList = New BooleanBisList(apres, True) e.Row.Cells(4).Controls.Add(list2) Case "Boolean" e.Row.Cells(2).Controls.Clear() Dim list3 As BooleanList = New BooleanList(avant, False) e.Row.Cells(2).Controls.Add(list3) e.Row.Cells(4).Controls.Clear() Dim list4 As BooleanList = New BooleanList(apres, True) e.Row.Cells(4).Controls.Add(list4) End Select在我的按鈕點擊事件中,我嘗試獲取使用者控制項:
Case "String" temp.ChampValeurApres = DirectCast(Tableau1.Rows(i).Cells(selectedColumn).Controls(1), TextBox).Text但我得到它不存在的錯誤。
您應該在RowCreated而不是RowDataBound中創建動態控制項,因為此事件會在每次回發時觸發,而
RowDataBound僅當數據GridView綁定到它時才會觸發DataSource。必須在每次回發時使用與以前相同的 ID 重新創建動態創建的控制項,然後它們在ViewState中保留它們的值,並且事件將正確觸發(例如 DropDownList 的
SelectedIndexChanged事件)。所以你應該創建它們
RowCreated並“填充”它們RowDataBound(feDropDownListdatasource/Items 或TextBox-Text)。