Hi All,<BR><BR>I have a problem with a DataList in a UserControl (code below). This DataList SHOULD display the three names in ArrayList "myAL" as soon as MainPage.aspx is loaded into the browser, but it does NOT.<BR><BR>However, strangely, when the LinkButton in the UserControl is clicked, the three names in ArrayList "myAL" AND the three names in ArrayList "MyAL2" are displayed. <BR><BR>I hope someone out there can explain why the "myAL" names aren't displayed as soon as the page renders - this is what I was trying to achieve.<BR><BR>Thanks in advance,<BR><BR>JON<BR><BR><BR>PS This posting is a much briefer version of the (unanswered) one titled "Why won't DataList display when page renders?", sent to this forum on 5/17/2002. <BR><BR><BR><BR><BR><BR><BR>++++++++++++++++++++++++<BR>MainPage.aspx<BR>++++++++++++++++++++++++<BR><BR><BR><BR><%@ Page Language="VB" debug="true" trace="true" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><%@ Register TagPrefix="User" TagName="Control" src=http://aspmessageboard.com/archive/index.php/"UserControl.ascx" %><BR><BR><BR><script language="VB" runat="server"><BR><BR>Sub Page_PreRender(obj as object, e as EventArgs)<BR><BR>If not Page.IsPostBack then<BR> <BR> Dim myAL As New ArrayList()<BR> myAL.Add("Bob")<BR> myAL.Add("Jimmy")<BR> myAL.Add("Eric")<BR> <BR> '*** The names Bob, Jimmy and Eric<BR> '*** should be visible when this page first displays<BR> '*** but they are not<BR> '*** WHY??????<BR> '*** These names DO become visible, however<BR> '*** when the LinkButton is clicked<BR> '*** WHY??????<BR> <BR> Dim myEnumerator As System.Collections.IEnumerator = myAL.GetEnumerator()<BR> While myEnumerator.MoveNext()<BR> UserControl.AddPerson(myEnumerator.Current, 1)<BR> End While<BR><BR>end if<BR><BR>end sub<BR><BR><BR>Public Sub DisplayNames(obj as object, e as eventargs)<BR><BR> Dim myAL2 As New ArrayList()<BR> myAL2.Add("Michael")<BR> myAL2.Add("Jack")<BR> myAL2.Add("Earl")<BR><BR> '*** This subroutine works normally<BR> '*** displaying the names Michael, Jack and Earl<BR> '*** every time the LinkButton is clicked<BR><BR> Dim myEnumerator As System.Collections.IEnumerator = myAL2.GetEnumerator()<BR> While myEnumerator.MoveNext()<BR> UserControl.AddPerson(myEnumerator.Current, 1)<BR> End While<BR><BR>End Sub<BR><BR><BR><BR></script><BR><BR><BR><html><BR><form runat="server"><BR><BR> <User:Control runat="server" id="UserControl" /><BR><BR> <asp:LinkButton text="click" runat="server" OnClick="DisplayNames" /><BR><BR></form><BR></html><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>++++++++++++++++++++++++<BR>UserControl.ascx<BR>++++++++++++++++++++++++<BR><BR><BR><BR><%@ Control Language="VB" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><script language="VB" runat="server"><BR><BR>Dim myDataTable as DataTable<BR><BR>Sub Page_Load(obj as object, e as eventargs)<BR><BR> If Not IsNothing(ViewState("PeopleToAdd")) then<BR> myDataList.DataSource = ViewState("PeopleToAdd")<BR> DataBind()<BR> end if<BR> <BR> If not Page.IsPostBack<BR> '*** Set up the DataTable "PeopleToAdd" in memory<BR> myDataTable = new DataTable("PeopleToAdd")<BR> Dim myColumn as DataColumn<BR> myColumn = myDataTable.Columns.Add("PersonID", System.Type.GetType("System.Int32"))<BR> myColumn.AllowDBNull = false<BR> myColumn = myDataTable.Columns.Add("Name", System.Type.GetType("System.String"))<BR> myColumn.AllowDBNull = false<BR> ViewState("PeopleToAdd") = myDataTable<BR> DataBind()<BR> end if <BR><BR>end sub<BR><BR><BR>Public Sub AddPerson(SelectedPersonName as String, SelectedPersonValue as Integer) <BR><BR> '*** Pull the DataTable out of the ViewState<BR> myDataTable = ViewState("PeopleToAdd")<BR> <BR> '*** Add the row to the DataTable<BR> Dim myRow as DataRow<BR> myRow = myDataTable.NewRow()<BR> myRow("PersonID") = SelectedPersonValue<BR> myRow("Name") = SelectedPersonName<BR> myDataTable.Rows.Add(myRow)<BR> <BR> '*** Put everything back in the ViewState <BR> ViewState("PeopleToAdd") = myDataTable<BR><BR> myDataList.DataBind()<BR> <BR>End sub<BR><BR><BR><BR></script><BR><BR><asp
ataList runat="server" id="myDataList" <BR> repeatlayout = "flow" repeatdirection = "horizontal"><BR><BR><ItemTemplate><BR> <asp:LinkButton runat="server" Text='<%# Container.DataItem("Name") %>'/><BR></ItemTemplate> <BR><BR></asp
ataList><BR>Hi All,<BR><BR>I solved the problem eventually. InUserControl.ascx, I had to move the line: <BR><BR> myDataList.DataSource = ViewState("PeopleToAdd")<BR><BR>into the Page_Load If Not Page.IsPostBack subroutine, giving this:<BR><BR> <BR>If not Page.IsPostBack<BR> <BR> myDataTable = new DataTable("PeopleToAdd")<BR> myColumn = myDataTable.Columns.Add("PersonID", System.Type.GetType("System.Int32"))<BR> myColumn.AllowDBNull = false<BR> myColumn = myDataTable.Columns.Add("Name", System.Type.GetType("System.String"))<BR> myColumn.AllowDBNull = false<BR> ViewState("PeopleToAdd") = myDataTable<BR> myDataList.DataSource = ViewState("PeopleToAdd") '**** PUT THE LINE HERE INSTEAD!<BR> DataBind()<BR> <BR>end if <BR><BR><BR>Cheers,<BR><BR>JON

