Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

asp.net - 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 ?

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...