Embedded User Control in DataList - Postback Issue

R

Rick Wez

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
 

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,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top