load dynamic control from a dropdownlist event handler, how to preserve the control after button event
I understand I need to load my dynmaic control on every postback and preferrably in Page_Init(). But my dyanmic controls are loaded from a dropdownlist selectedindexchanged event handler. Now after any postback, my dynamic controls are gone. How would I force it to load on every postback ?
T开发者_JS百科hanks a lot!
You have to store the number of created controls somewhere. I recommend ViewState for this purpose. Then you have to recreate the dynamically created controls on every postback with the same id as before. Because ViewState
is available in Page_Load
i would use this event. In your DropdownList's SelectedIndexChanged
-Eventhandler you add the controls and increase the ViewState
-Variable accordingly.
Here is an example:
aspx:
<asp:placeholder ID="Container" runat="server"></asp:placeholder><br/>
<asp:Label ID="LblInfo" runat="server"></asp:Label><br />
<asp:DropDownList ID="DdlAddControls" OnSelectedIndexChanged="DdlAddControls_SelectedIndexChanged" AutoPostBack="true" runat="server">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="add 1 button" Value="1"></asp:ListItem>
<asp:ListItem Text="add 2 buttons" Value="2"></asp:ListItem>
<asp:ListItem Text="add 3 buttons" Value="3"></asp:ListItem>
</asp:DropDownList>
VB.Net
Private Property ControlCount As Int32
Get
If ViewState("ControlCount") Is Nothing Then
ViewState("ControlCount") = 0
End If
Return DirectCast(ViewState("ControlCount"), Int32)
End Get
Set(ByVal value As Int32)
ViewState("ControlCount") = value
End Set
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ControlCount <> 0 Then
RecreateControls()
End If
End Sub
Private Sub RecreateControls()
addControls(ControlCount)
End Sub
Private Sub addControls(ByVal count As Int32)
For i As Int32 = 1 To count
Dim btn As New Button() ' i use a Button as example'
btn.ID = "Button_" & Me.Container.Controls.Count
btn.Text = "Button " & Me.Container.Controls.Count
AddHandler btn.Click, AddressOf Click
Me.Container.Controls.Add(btn)
Next
End Sub
Private Sub Click(ByVal sender As Object, ByVal e As EventArgs)
LblInfo.Text = DirectCast(sender, Button).Id & "clicked"
End Sub
Protected Sub DdlAddControls_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlAddControls.SelectedIndexChanged
Dim newControlCount As Int32 = Int32.Parse(DdlAddControls.SelectedValue)
addControls(newControlCount)
ControlCount += newControlCount
End Sub
C#
private Int32 ControlCount {
get {
if (ViewState("ControlCount") == null) {
ViewState("ControlCount") = 0;
}
return (Int32)ViewState("ControlCount");
}
set { ViewState("ControlCount") = value; }
}
private void Page_Load(object sender, System.EventArgs e)
{
if (ControlCount != 0) {
RecreateControls();
}
}
private void RecreateControls()
{
addControls(ControlCount);
}
private void addControls(Int32 count)
{
for (Int32 i = 1; i <= count; i++) {
Button btn = new Button();
// i use a Button as example'
btn.ID = "Button_" + this.Container.Controls.Count;
btn.Text = "Button " + this.Container.Controls.Count;
btn.Click += Click;
this.Container.Controls.Add(btn);
}
}
private void Click(object sender, EventArgs e)
{
LblInfo.Text = ((Button)sender).Id + "clicked";
}
Protected void DdlAddControls_SelectedIndexChanged(object sender, System.EventArgs e)
{
Int32 newControlCount = Int32.Parse(DdlAddControls.SelectedValue);
addControls(newControlCount);
ControlCount += newControlCount;
}
精彩评论