Asp.net Ajax reloads entire class

Discussion in 'ASP .Net' started by Oitne.Inside@gmail.com, Sep 20, 2007.

  1. Guest

    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
     
    , Sep 20, 2007
    #1
    1. Advertising

  2. 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.


    --
    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    <> wrote in message
    news:...
    > 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
    >
     
    Mark Fitzpatrick, Sep 20, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian
    Replies:
    0
    Views:
    395
    Christian
    Jul 17, 2003
  2. VB Programmer

    Why swf reloads every time?

    VB Programmer, May 13, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    374
    VB Programmer
    May 13, 2005
  3. SevDer

    AppDomain randomly reloads

    SevDer, Jan 4, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,070
    SevDer
    Jan 4, 2006
  4. Markus Feißt

    Page reloads always

    Markus Feißt, Jul 22, 2003, in forum: HTML
    Replies:
    1
    Views:
    380
    Mental Patient 0057
    Jul 22, 2003
  5. Merlin
    Replies:
    1
    Views:
    417
    Jeff Thies
    Apr 16, 2004
Loading...

Share This Page