Dynamic Contrl Generation Problem

PatelNehal4u

Active member
Joined
Apr 6, 2009
Messages
25
Programming Experience
1-3
Hello Everyone,

I am new to asp.net and i had created a form with a button, which create a new button dynamically, i bind that button with an event but when i press newly created button it again got deleted and no response from it. Here is the coding.
VB.NET:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim btn As New Button
        btn.Text = "My New Button"
        btn.EnableViewState = True
        Panel1.Controls.Add(btn)
        AddHandler btn.Click, AddressOf btn_click
    End Sub

    Protected Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Write("Button Clicked")
    End Sub
 
The page is recreated each request. You have a button click request that creates a new button. No other type of request will create that button. So when you click the other button, or make any other postback, that button will not be created again.
You need to look into the possibilities you have to maintain state in ASP.Net environment and work with that: ASP.NET State Management Overview
Then you have to find a place in the loading process of the page that allow you to recreate the dynamic control for every request: ASP.NET Page Life Cycle Overview
 

Latest posts

Back
Top