Embedded User Control in DataList - Postback Issue

Discussion in 'ASP .Net' started by Rick Wez, Mar 17, 2006.

  1. Rick Wez

    Rick Wez Guest

    I feel like I'm going to pull out my hair (there's not much left); I've
    read almost every online post trying to figure this out. So I started
    over with something VERY simple and it still doesn't work ... here we
    go. I have a datalist with an embedded user control. My user control
    outputs a single string. The first time thru, everything works
    perfectly ... on a postback, the datalist gets it's values correctly,
    but the user control does NOT get a passed in value ... here is my test
    code; it can't be much simpler (I know that this has to do something
    with the ordering of events).

    -- TestClass.ascx --
    <%@ Control Language="VB" AutoEventWireup="false" ClassName="TestClass"
    %>
    <script language="VB" runat="server">
    Public MyString As String
    Private Sub Page_Load(ByVal src As Object, ByVal e As EventArgs)
    Handles MyBase.Load
    Response.Write(MyString)
    End Sub
    </script>

    -- TestPage.aspx --
    <%@ Page Language="VB" AutoEventWireup="false" Trace="true"%>
    <%@ Register TagPrefix="MyTest" TagName="TestClass"
    Src="TestClass.ascx" %><html>
    <head runat="server">
    <script language="VB" runat="server">
    Sub Page_Load(ByVal src As Object, ByVal e As EventArgs)
    Handles MyBase.Load
    If Not IsPostBack Then
    Dim dt As New System.Data.DataTable
    dt.Columns.Add("name")
    Dim dr As System.Data.DataRow
    dr = dt.NewRow
    dr.Item(0) = "First Run"
    dt.Rows.Add(dr)
    dg.DataSource = dt
    dg.DataBind()
    End If
    End Sub
    Sub linkBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
    Trace.Warn("Clicked Button Event")
    Dim dt As New System.Data.DataTable
    dt.Columns.Add("name")
    Dim dr As System.Data.DataRow
    dr = dt.NewRow
    dr.Item(0) = "Second Run"
    dt.Rows.Add(dr)
    dg.DataSource = dt
    dg.DataBind()
    End Sub
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:LinkButton ID="linkBtn" runat="server"
    OnClick="linkBtn_Click">Click</asp:LinkButton>
    <asp:DataList ID="dg" runat="server" >
    <ItemTemplate>
    <%# Container.DataItem("name") %> in datalist
    <MyTest:TestClass id="lalala" runat="server" MyString = '<%#
    Container.DataItem("name") %>' />
    </ItemTemplate>
    </asp:DataList>
    </form>
    </body>
    </html>

    Can't be much made any more simple ... TestClass takes MyString and
    spits it out to Response.Write. Problem:
    1) On first run, everything works perfectly
    2) Click the "Click" button
    3) I get my "Second Run in datalist" line, but MyString in the user
    control isn't being rebound. I know that Container.DataItem HAS a
    value and is NOT null.

    I don't care about a Viewstate (because I always want to recreate this
    control regardless). I don't plan on retrieving any information from
    the control (it is just a display control, I'd use an include file like
    in PHP or HTML if I could ... we are trying to modularize code rather
    than have 1000+ lines in one file)

    Please can anyone help!!!!

    Rick
    Rick Wez, Mar 17, 2006
    #1
    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. Replies:
    1
    Views:
    3,124
  2. Nevyn Twyll
    Replies:
    8
    Views:
    1,867
  3. Mad Scientist Jr
    Replies:
    0
    Views:
    633
    Mad Scientist Jr
    Mar 22, 2006
  4. Thomas Ekegren

    Page Load fired 3 times Web user control is embedded in a custom control

    Thomas Ekegren, Sep 3, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    221
    Thomas Ekegren
    Sep 3, 2003
  5. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    394
    Thomas Dodds
    Jul 26, 2004
Loading...

Share This Page