I am looking to update one of my DDL's functionality by making it dynamically update so if the user adds more files, the drop down will pick this up.
At present my drop down list is pulling from VB code behind, as shown below:
Public Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList) 'item is already dropdownlist
Dim ctl As TextBox = DirectCast(ddl.NamingContainer.FindControl("eTemplate"), TextBox)
If ddl.SelectedValue = 1 Then
ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Down.txt")
ElseIf ddl.SelectedValue = 2 Then
ctl.Text = File.ReadAllText("e:Documents\Visual Studio 2013\Projects\Web\Templates\Up.txt")
Else
ctl.Text = ""
End If
End Sub
At the moment I have hard coded in the functionality for the VB to grab specific .txt files, how can I get this to update dynamically from a folder of .txt files?
Thanks for looking.
Here is some sample code for you. This demo uses an UpdatePanel and a Timer to refresh the DropdownList every 5 seconds.
Add a new aspx file to your Web Application and the following code:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Zpk_Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Asynchronous Update Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" /><br />
<asp:Timer runat="server" ID="Timer1" Interval="5000" Enabled="true" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="DropDownList1" />
</Triggers>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300" Height="250" />
</form>
</body>
</html>
This is the code-behind:
Partial Class Demo
Inherits System.Web.UI.Page
Private Const FolderName As String = "C:\Temp" '<-- replace with your folder name
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
RefreshDropDownList()
OpenSelectedFile()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' this event is fired everytime a timer ticks.
' refresh your dropdown list here.
RefreshDropDownList()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
OpenSelectedFile()
End Sub
Private Sub RefreshDropDownList()
Dim currentSelected As String = DropDownList1.SelectedValue
DropDownList1.DataSource = IO.Directory.GetFiles(FolderName, "*.txt").Select(Function(f) IO.Path.GetFileName(f)).ToList
DropDownList1.DataBind()
DropDownList1.SelectedValue = currentSelected
End Sub
Private Sub OpenSelectedFile()
Dim fileName As String = IO.Path.Combine(FolderName, DropDownList1.SelectedValue)
TextBox1.Text = IO.File.ReadAllText(fileName)
End Sub
End Class
Related
I am using Visual Studio 2010, VB.NET, target framework .NET 4.0.
I have a GridView which I am binding to some object collection, with a CommandField column that should allow edit of the selected row.
I am setting the EditIndex property correctly in the RowEditing eventhandler.
The problem is: When I click the "Edit" link that is generated, nothing apparently happens, the row gets rendered again in "view mode", not "edit mode".
But if I do some random postback, like clicking my "doNothing" button, the row gets rendered in "edit mode" in the next postback.
I have managed to reproduce the problem in the following code:
ASPX:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="MyForm.aspx.vb" Inherits="MySandBox.MyForm" %>
<%# Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvInfrator" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField HeaderText="MyField" DataField="MyField" />
</Columns>
</asp:GridView>
<asp:Button ID="btnDoNothing" runat="server" Text="Just do a postback" />
</form>
</body>
</html>
Code Behind:
Public Class MyDto
Public Property MyField As String
End Class
Public Class MyForm
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.BindMyData()
End Sub
Private Sub BindMyData()
Dim myData As New MyDto
myData.MyField = "My field value"
Me.gvInfrator.DataSource = New MyDto() {myData}
Me.gvInfrator.DataBind()
End Sub
Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
Me.BindMyData()
Me.gvInfrator.EditIndex = e.NewEditIndex
End Sub
End Class
Am I doing something wrong? Is this a bug in .NET Framework 4? Is there any workaround?
You need to bind your data after you set the EditIndex of your GridView.
Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
Me.gvInfrator.EditIndex = e.NewEditIndex
Me.BindMyData()
End Sub
Ok, I have been going around and around with this for hours (Searching, etc). This is what I want to do, I want to load data into a Text Box and if the user changes the text in the Text Box I want to be able to save the new text.
My problem in the TxtBox_TextChanged event the data contained in the txtNarrative Text box is the new data that the user typed in (<>ABCD) but in the btnSubmit_Click event the data contained in txtNarrative is the original value ABCD.
What am I doing wrong??
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WorkBench_VBNet._Default" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<span class="title">Entry Form</span>
<ul class="pageitem">
<li class="Narrative">
<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%"
Rows="10" TextMode="multiline" runat="server" Height = "100%" OnTextChanged="TxtBox_TextChanged" >
</asp:TextBox></li>
<li class="Submit">
<asp:LinkButton ID="btnSubmit" runat="server">Submit</asp:LinkButton>
</li>
</ul>
</fieldset>
</div>
</form>
</body>
</html>
Code Behind:
Public Class _Default
Inherits System.Web.UI.Page
Public Event TextChanged As EventHandler
Protected Sub TxtBox_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtNarrative.TextChanged
ViewState("txtNarrative") = txtNarrative.Text ''<-- The text here is the changed text not ABCD
txtNarrative.Text = ViewState("txtNarrative").ToString
End Sub
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
Dim Narrative as String = txtNarrative.Text '<-- the text in the text box is still ABCD not what was changed.
''Code to update data in the Database goes here
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
txtNarrative.Text = "ABCD"
End If
End Sub
End Class
Get rid of the TxtBox_TextChanged Sub, it's not needed, the IPostBackDataHandler of the TextBox will do that for you:
<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%"
Rows="10" TextMode="multiline" runat="server" Height = "100%" >
</asp:TextBox>
I'm not sure if I understand your question but:
You do not need to "OnTextChanged" textbox in your markup.
In your code behind you alredy have handles declaration.
If you use both ("OnTextChanged" on your html markup, and handles in your code behind) the text box event will trigger twice.
Protected Sub TxtBox_TextChanged (ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtNarrative.TextChanged
ViewState ("txtNarrative") = txtNarrative.Text''<-- The text here text is not changed the ABCD
txtNarrative.Text = ViewState ("txtNarrative"). ToString
end Sub
<asp: TextBox EnableViewState = true ID = "txtNarrative" placeholder = "Narrative" Width = "100%"
Rows = "10" TextMode = "multiline" runat = "server" Height = "100%">
</ asp: TextBox>
also tested your code and the value that is submitted is the new value in the textbox. (Ie if I understand your question)
Hope this helps
Well I have a strange problem.
I have a page with 2 DropDownLists on it and a custom web-user-control. The custom web user control has an UpdatePanel within it, and an Ajax Timer control within the UpdatePanel to periodically update a listing of stuff.
When I "drop-down" one of the DropDownLists and hover over (not click on) an option while the Timer control within the UpdatePanel asynchronously posts back to the server, the DropDownList "autopostbacks" to the server!
I'm trying to figure out why an asynchronous postback would cause the DropDownList to act as if I selected/clicked on an option so that I can find a way around this issue.
Now it's really simple to reproduce this problem.
Create a Web User control called "TimerUpdatedListing"...this is the ASPX code markup for the web user control:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="TimerUpdatedListing.ascx.vb" Inherits="MyNamespace.TimerUpdatedListing" %>
<div style="width: 150px; height: 150px; overflow: auto; border: solid 1px navy;">
<asp:UpdatePanel ID="anUpdatePanel" runat="server">
<ContentTemplate>
<asp:Repeater ID="aRepeater" runat="server">
<ItemTemplate>
<div style="border-bottom: solid 1px #EEC900; margin: 3px; padding: 2px;">
Id:
<%#Eval("Id")%>
<br />
Time:
<%#Eval("Time")%>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Timer ID="aTimer" runat="server" Interval="2000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
This is the VB.NET server side code for the web user control:
Public Partial Class TimerUpdatedListing
Inherits System.Web.UI.UserControl
Private _aListOFThings As List(Of Things)
Private Sub aTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles aTimer.Tick
If Session("_aListOfThings") Is Nothing Then
_aListOFThings = New List(Of Things)
Else
_aListOFThings = CType(Session("_aListOfThings"), List(Of Things))
End If
If _aListOFThings.Count > 9 Then
_aListOFThings = New List(Of Things)
End If
_aListOFThings.Add(New Things((_aListOFThings.Count + 1).ToString, Now.ToString("hh:mm:ss")))
Session("_aListOfThings") = _aListOFThings
aRepeater.DataSource = _aListOFThings
aRepeater.DataBind()
End Sub
Private Class Things
Private _time As String
Private _id As String
Public Property Time() As String
Get
Return _time
End Get
Set(ByVal value As String)
_time = value
End Set
End Property
Public Property ID() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Public Sub New(ByVal id As String, ByVal time As String)
_id = id
_time = time
End Sub
End Class
End Class
Now, in an ASPX page called WebForm1.aspx, add 2 DropDownLists and the web user control:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="MyNamespace.WebForm1" %>
<%# Register Src="TimerUpdatedListing.ascx" TagName="TimerUpdatedListing" TagPrefix="uc1" %>
<!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>Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Text="1" Value="1" />
<asp:ListItem Text="2" Value="2" />
<asp:ListItem Text="3" Value="3" />
<asp:ListItem Text="4" Value="4" />
<asp:ListItem Text="5" Value="5" />
</asp:DropDownList>
<asp:Label ID="selectedValue1" runat="server"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem Text="a" Value="a" />
<asp:ListItem Text="b" Value="b" />
<asp:ListItem Text="c" Value="c" />
<asp:ListItem Text="d" Value="d" />
<asp:ListItem Text="e" Value="e" />
</asp:DropDownList>
<asp:Label ID="selectedValue2" runat="server"></asp:Label>
<br />
<br />
<uc1:TimerUpdatedListing ID="TimerUpdatedListing1" runat="server" />
</div>
</form>
</body>
</html>
Here is the VB.NET server side code for the WebForm1.aspx page:
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then
selectedValue1.Text = Request.Params("ddl1")
End If
If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then
selectedValue2.Text = Request.Params("ddl2")
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True)
End Sub
Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True)
End Sub
End Class
Thanks,
-Frinny
Here is an example of using triggers to trigger your update panel. Put the timer.tick event as a trigger and see what happens
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers
PS: I don't think the timer has to be included within the update panel. I actually build my timers in the codebehind and keep them out of the markup all together.
I attempted two different solutions to this problem.
The first thing I did was to check the Request.Params("__EVENTTARGET") to see if it matched the DropDownList. If it matched then I would call the Response.Redirect() method.
For example:
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then
selectedValue1.Text = Request.Params("ddl1")
End If
If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then
selectedValue2.Text = Request.Params("ddl2")
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim ctrlNameThatCausedPostback As String = Request.Params("__EVENTTARGET")
If String.IsNullOrEmpty(ctrlNameThatCausedPostback) = False AndAlso Page.FindControl(ctrlNameThatCausedPostback) Is DropDownList1 Then
Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True)
End If
End Sub
Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
Dim ctrlNameThatCausedPostback As String = Request.Params("__EVENTTARGET")
If String.IsNullOrEmpty(ctrlNameThatCausedPostback) = False AndAlso Page.FindControl(ctrlNameThatCausedPostback) Is DropDownList2 Then
Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True)
End If
End Sub
End Class
I noticed that this didn't always work. There were times when I selected an option in the DropDownList and the Redirect wouldn't take place because the timer tick and the selected index changed event happened at the same time.
So the second approach I took was to check to see if the page is in an asynchronous postback to the server. If it was, then I knew that it was the timer tick event taking place and that the Redirect shouldn't occur.
For example:
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then
selectedValue1.Text = Request.Params("ddl1")
End If
If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then
selectedValue2.Text = Request.Params("ddl2")
End If
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack = False Then
Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True)
End If
End Sub
Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
If ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack = False Then
Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True)
End If
End Sub
End Class
This helped, and the likelihood that the Redirect would occur properly was better than the previous approach; however, it still isn't 100%.
-Frinny
I am trying to figure out how to declaratively pass in a event handler into
a user control, but I am stumped. All I can make work is the user control's
event handler.. I can't seem to bubble up the caught event into the parent
page. Ideas would be quite welcome. Here is my code:
Default.aspx:
<%# Page Language="VB" %>
<%# Register TagPrefix="rpt" TagName="filter" Src="WebUserControl.ascx" %>
<!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>Test Controls</title>
</head>
<body>
<form id="form1" runat="server">
<rpt:filter ID="DataView1Filters" runat="server" SelectedIndexChanged="DropDown_SelectedIndexChanged" />
<asp:Label ID="Label1" runat="server" />
</form>
<script runat="server">
Public Sub DropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = String.Format("Inside declarative event handler. {0}<br>", Label1.Text)
End Sub
</script>
</body>
</html>
WebUserControl.ascx:
<%# Control Language="VB" ClassName="WebUserControlTest" %>
<asp:Panel ID="TestPanel" runat="server"></asp:Panel>
<script runat="server">
Private AllEvents As New System.ComponentModel.EventHandlerList
Public Custom Event SelectedIndexChanged As EventHandler
AddHandler(ByVal value As EventHandler)
AllEvents.AddHandler("SelectedIndexChanged", value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
AllEvents.RemoveHandler("SelectedIndexChanged", value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
Dim value As EventHandler = CType(AllEvents("SelectedIndexChanged"), EventHandler)
If Not value Is Nothing Then
value.Invoke(sender, e)
End If
End RaiseEvent
End Event
Private Sub _SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ctrl As DropDownList = Me.FindControl("TestDropDownList")
If Not ctrl Is Nothing Then
Me.ViewState("ItemSelection") = ctrl.SelectedIndex
End If
Dim Label1 As Label = Parent.FindControl("Label1")
Label1.Text = String.Format("Inside user control event handler. {0}<br>", Label1.Text)
RaiseEvent SelectedIndexChanged(sender, e)
End Sub
Private Overloads Sub OnLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ctrl As New DropDownList
With ctrl
.ID = "TestDropDownList"
.Items.Clear()
.AutoPostBack = True
AddHandler .SelectedIndexChanged, AddressOf _SelectedIndexChanged
.Items.Add(New ListItem("-- Select --", String.Empty))
.Items.Add(New ListItem("Item 1", "1"))
.Items.Add(New ListItem("Item 2", "2"))
If Not Me.ViewState("ItemSelection") Is Nothing Then
.SelectedIndex = CInt(Me.ViewState("ItemSelection"))
Else
.SelectedIndex = 0
End If
End With
TestPanel.Controls.Add(ctrl)
End Sub
</script>
Thanks!
See this previous post:
Handling User Control Events on Containing Page
Edit - added based on your comment
I should have read the question more clearly.
As far as having a UserControl raise an event that the containing page can respond to, I do not believe that this can be done declaratively.
Unless my knowledge is just lacking, the only way to accomplish this is by explicitly creating an event in the control and then handling it (by coding the event handler) on the parent page, as shown in the example I linked to.
I was recently having this same issue in C#. When you set up an event called SelectedIndexChanged asp.net will bind the attribute OnSelectedIndexChanged when using the declarative syntax.
So if you change
<rpt:filter ID="DataView1Filters" runat="server" SelectedIndexChanged="DropDown_SelectedIndexChanged" />
To
<rpt:filter ID="DataView1Filters" runat="server" OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
It should work.
I've been racking my brain trying to get this to work. My event for my LinkButton isn't firing. I'm figuring that it has SOMETHING to do with ViewState and that the button is not there when it tries to fire the event after the Postback or something.
When I click the Add button,it adds the link to the page and then when I click the Diplay Time" linkbutton it should fire the event and display the CommandArgument data but it's not and i can't figure out why.
Here's my code:
<%# Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Protected Sub btnDelete_OnCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Response.Write(e.CommandArgument)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As New LinkButton()
btn.ID = "lbn"
btn.Text = "Display Time"
btn.ValidationGroup = "vgDeleteSigner"
AddHandler btn.Command, AddressOf btnDelete_OnCommand
btn.CommandArgument = Now.TimeOfDay.ToString()
btn.EnableViewState = True
Panel1.Controls.Add(btn)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
The reason that's happening is that your dynamic button "lbn" needs to be drawn again on post back when it's clicked because the button doesn't exist after you click it.
Basically you just have to dynamically add the button to the page again on post back of click of that button.
I would recommend having the button already on the page but visible = false and then just showing it when you click the other button.