|
 |
aspx_beginners thread: DataList problem
Message #1 by "Jon Maz" <jonmaz@s...> on Sun, 19 May 2002 19:36:07
|
|
Hi All,
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.
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.
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.
Thanks in advance,
JON
PS This posting is a much briefer version of the one titled "Why won't
DataList display when page renders?", which I sent in the early hours of
yesterday morning (18.05.02). That posting is probably much too long for
anyone to actually have the patience to read through it, hence this
simplified version!
++++++++++++++++++++++++
MainPage.aspx
++++++++++++++++++++++++
<%@ Page Language="VB" debug="true" trace="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<%@ Register TagPrefix="User" TagName="Control" src="UserControl.ascx" %>
<script language="VB" runat="server">
Sub Page_PreRender(obj as object, e as EventArgs)
If not Page.IsPostBack then
Dim myAL As New ArrayList()
myAL.Add("Bob")
myAL.Add("Jimmy")
myAL.Add("Eric")
'*** The names Bob, Jimmy and Eric
'*** should be visible when this page first displays
'*** but they are not
'*** WHY??????
'*** These names DO become visible, however
'*** when the LinkButton is clicked
'*** WHY??????
Dim myEnumerator As System.Collections.IEnumerator = myAL.GetEnumerator()
While myEnumerator.MoveNext()
UserControl.AddPerson(myEnumerator.Current, 1)
End While
end if
end sub
Public Sub DisplayNames(obj as object, e as eventargs)
Dim myAL2 As New ArrayList()
myAL2.Add("Michael")
myAL2.Add("Jack")
myAL2.Add("Earl")
'*** This subroutine works normally
'*** displaying the names Michael, Jack and Earl
'*** every time the LinkButton is clicked
Dim myEnumerator As System.Collections.IEnumerator = myAL2.GetEnumerator
()
While myEnumerator.MoveNext()
UserControl.AddPerson(myEnumerator.Current, 1)
End While
End Sub
</script>
<html>
<form runat="server">
<User:Control runat="server" id="UserControl" />
<asp:LinkButton text="click" runat="server" OnClick="DisplayNames" />
</form>
</html>
++++++++++++++++++++++++
UserControl.ascx
++++++++++++++++++++++++
<%@ Control Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Dim myDataTable as DataTable
Sub Page_Load(obj as object, e as eventargs)
If Not IsNothing(ViewState("PeopleToAdd")) then
myDataList.DataSource = ViewState("PeopleToAdd")
DataBind()
end if
If not Page.IsPostBack
'*** Set up the DataTable "PeopleToAdd" in memory
myDataTable = new DataTable("PeopleToAdd")
Dim myColumn as DataColumn
myColumn = myDataTable.Columns.Add("PersonID", System.Type.GetType
("System.Int32"))
myColumn.AllowDBNull = false
myColumn = myDataTable.Columns.Add("Name", System.Type.GetType
("System.String"))
myColumn.AllowDBNull = false
ViewState("PeopleToAdd") = myDataTable
DataBind()
end if
end sub
Public Sub AddPerson(SelectedPersonName as String, SelectedPersonValue as
Integer)
'*** Pull the DataTable out of the ViewState
myDataTable = ViewState("PeopleToAdd")
'*** Add the row to the DataTable
Dim myRow as DataRow
myRow = myDataTable.NewRow()
myRow("PersonID") = SelectedPersonValue
myRow("Name") = SelectedPersonName
myDataTable.Rows.Add(myRow)
'*** Put everything back in the ViewState
ViewState("PeopleToAdd") = myDataTable
myDataList.DataBind()
End sub
</script>
<asp:DataList runat="server" id="myDataList"
repeatlayout = "flow" repeatdirection = "horizontal">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Container.DataItem("Name") %>'/>
</ItemTemplate>
</asp:DataList>
Message #2 by "Jon Maz" <jonmaz@s...> on Thu, 23 May 2002 22:35:37
|
|
Hi All,
I solved the problem eventually. InUserControl.ascx, I had to move the
line:
myDataList.DataSource = ViewState("PeopleToAdd")
into the Page_Load If Not Page.IsPostBack subroutine, giving this:
If not Page.IsPostBack
myDataTable = new DataTable("PeopleToAdd")
myColumn = myDataTable.Columns.Add("PersonID", System.Type.GetType
("System.Int32"))
myColumn.AllowDBNull = false
myColumn = myDataTable.Columns.Add("Name", System.Type.GetType
("System.String"))
myColumn.AllowDBNull = false
ViewState("PeopleToAdd") = myDataTable
'**** PUT THE LINE HERE INSTEAD!
myDataList.DataSource = ViewState("PeopleToAdd")
DataBind()
end if
Cheers,
JON
|
|
 |