First, let me start off by saying I am using ASP.NET 2.0 with VB as the default language using Visual Studio 2005.

I have a form that has an asp:panel on it that is seeded with an iniitial control. Through the use of the form, additional controls may be dynamically added to the form. The problem is that none of the dynamically added controls persist on a postback. Here's some example code the exhibits the behavior I am seeing.

First, the HTML:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Panel ID="divNav" runat="server" EnableViewState="true" Visible="true">
                <asp:LinkButton ID="lbHome" runat="server">0</asp:LinkButton>
            </asp:Panel>
            <asp:Button ID="Button1" runat="server" Text="Add Another Line" />
        </form>
    </body>
</html>

And now, the code behind:

Partial Class test2
    Inherits System.Web.UI.Page

    Private intLineNum As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            intLineNum = 1
        Else
            intLineNum += 1
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lb As New LinkButton
        lb.ID = "lb" & intLineNum
        lb.Text = intLineNum
        Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, New LiteralControl("<br />"))
        Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, (lb))

    End Sub

End Class

What happens is that when you click the button to add another LinkButton, it does indeed add a new LinkButton. The problem is that it won't add any others beyond that. I've tried both the Load and Pre_Init events for the page, but my code does not work in either of them.

I've also tried getting a count of the controls on the panel and using the AddAt method to add the new controls at the end of the array, but again I cannot find a way to make that work as it only ever sees at most two controls.

Can someone help out with this? Thanks.

>Controls added to asp:panel not persisting on PostBack

And that is the main reason that one should have to use page_load or page_init event to add controls dynamically.

First, let me start off by saying I am using ASP.NET 2.0 with VB as the default language using Visual Studio 2005.

I have a form that has an asp:panel on it that is seeded with an iniitial control. Through the use of the form, additional controls may be dynamically added to the form. The problem is that none of the dynamically added controls persist on a postback. Here's some example code the exhibits the behavior I am seeing.

First, the HTML:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Panel ID="divNav" runat="server" EnableViewState="true" Visible="true">
                <asp:LinkButton ID="lbHome" runat="server">0</asp:LinkButton>
            </asp:Panel>
            <asp:Button ID="Button1" runat="server" Text="Add Another Line" />
        </form>
    </body>
</html>

And now, the code behind:

Partial Class test2
    Inherits System.Web.UI.Page

    Private intLineNum As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            intLineNum = 1
        Else
            intLineNum += 1
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lb As New LinkButton
        lb.ID = "lb" & intLineNum
        lb.Text = intLineNum
        Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, New LiteralControl("<br />"))
        Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, (lb))

    End Sub

End Class

What happens is that when you click the button to add another LinkButton, it does indeed add a new LinkButton. The problem is that it won't add any others beyond that. I've tried both the Load and Pre_Init events for the page, but my code does not work in either of them.

I've also tried getting a count of the controls on the panel and using the AddAt method to add the new controls at the end of the array, but again I cannot find a way to make that work as it only ever sees at most two controls.

Can someone help out with this? Thanks.

to add control dynamically you can use javascript or jquery thats most effective. if you think you start working with javascript or jquery i help you?
b'coz server side postback load whole html page and request is http so http is stateless protocol whole page request to server side that's why that's happening. the solution is use jquery or javascript....

And that is the main reason that one should have to use page_load or page_init event to add controls dynamically.

Fair enough. I altered my code behind as follows:

Partial Class test2
    Inherits System.Web.UI.Page

    Private intLineNum As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            intLineNum = 1
        Else
            intLineNum = intLineNum + 1
            Dim lb As New LinkButton
            lb.ID = "lb" & intLineNum
            lb.Text = intLineNum
            Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, New LiteralControl("<br />"))
            Me.divNav.Controls.AddAt(Me.divNav.Controls.Count, (lb))
        End If
    End Sub

End Class

But it's exhibiting the same behavior. I tried moving the code to both Init and Pre_Init as well, but with the same results. Do you have another suggestion?

b'coz server side postback load whole html page and request is http so http is stateless protocol whole page request to server side that's why that's happening. the solution is use jquery or javascript....

I can do javascript programming (never tried jquery), but the actual page this snippet was taken from has a lot of server-side code already and I thought maybe I could just add this in and not have to worry about secure (that is, script-disabled) browsing.

Nothing else I'm doing with the page would interfere with the problem I'm presenting here. The HTML and VB code I provided above illustrate my problem in a stand-alone setting. Any method to correct this stand alone example would work in my real page without interfering with any thing else I'm doing on the server.

I do thank you for pointing out the javascript possibility, though. I can certainly fall back on that if I can't find an acceptable solution for the server-side.

Well, time to give up on an elegant solution. I've resorted to using a viewstate variable and a for...next loop to handle the dynamic controls. Just in case anyone else needs a solution to a similar problem, here is what worked for me:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test3.aspx.vb" Inherits="test3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Dynamic Controls - PostBack Demo</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:PlaceHolder ID="PlaceHolder1" runat="server">
                <asp:LinkButton ID="LinkButton1" runat="server">Start</asp:LinkButton>
            </asp:PlaceHolder>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Add New Link Button" />
        </form>
    </body>
</html>

And here's the code behind:

Partial Class test3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim i As Integer
        For i = 1 To Val(ViewState("COUNT"))
            Dim txt As New LinkButton
            txt.ID = "Text" & i.ToString()
            txt.Text = i.ToString()
            PlaceHolder1.Controls.Add(New LiteralControl("&nbsp;&gt;&nbsp;"))
            PlaceHolder1.Controls.Add(txt)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lb As New LinkButton
        Viewstate("COUNT") += 1
        lb.ID = "Text" & ViewState("COUNT")
        lb.Text = ViewState("COUNT")
        PlaceHolder1.Controls.Add(New LiteralControl("&nbsp;&gt;&nbsp;"))
        PlaceHolder1.Controls.Add(lb)
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.