Asp.net Ajax reloads entire class

O

Oitne.Inside

Hello there,

We are trying to make a asp.net ajax application. For testing purposes
we have created a small program which has to create buttons who will
create more buttons when they are clicked.

Here's our code.

<%@ Page Language="VB" AutoEventWireup="true"
CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/
TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
UpdateMode="Always">

<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server"></
asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></
asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" EnableViewState="false" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim b As New Button
b.Text = "Nieuweknop"
AddHandler b.Click, AddressOf Button1_Click
Me.UpdatePanel1.ContentTemplateContainer().Controls.Add(b)
End Sub
End Class


As you can see, when the button is clicked, it should make a new
button. This works, but when we click the same button again, nothing
happends. We think the class is being reloaded.

When we click the newly created button, the button disapperates.
Anyone got any idea what where missing?

Thanks,

Jeremy
 
M

Mark Fitzpatrick

I think you're running into the old State Management issue. Keep in mind,
you're dynamically creating items. That doesn't mean they automatically get
tracked by the viewstate. What is happening is the control is not being
recreated each time the page reloads (AJAX panel or no) because it is not
being kept in the viewstate. Get this working like a regular ASP.Net page
first. What a lot of people get wrong is what AJAX is doing for you here. It
is not changing the fundamental way the ASP.Net page works, it's changing
how items are updated. The page still has the same flow it just means that
when an event is triggered from within the AJAX panel, it can only visually
update those items within the panel and not those outside it (though you can
still perform other operations that could effect things outside the panel,
they just don't get updated visually).

The issue here is one of state management. You need to do some research on
dynamically created controls as they're awefully tricky to get just right.
Get it correct and then get it into the AJAX panel as the AJAX panel will
just sidetrack you to start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top