A
Arpan
The following code retrieves records from a database table & displays
it in a Repeater control:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
sqlConn = New SqlConnection("Data
Source=MyDS\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users",
sqlConn)
dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")
rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"
rptrUsers.DataBind()
sqlConn.Close()
End If
End Sub
Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Data Bound<hr>")
End Sub
Sub ItemCreated(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item Created<br>")
End Sub
</script>
<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCreated="ItemCreated"
OnItemDataBound="BindData" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>NAME</th>
<th>PHONE NO.</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="linkbut" runat="server"><%#
Container.DataItem("UName") %></asp:LinkButton>
</td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td><%# Container.DataItem("UName") %></td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
Note that the records under the "NAME" column within the <ItemTemplate>
are hyperlinks whereas the records under the "NAME" column within the
<AlternatingItemTemplate> aren't hyperlinks. Assume that the Repeater
displays 4 rows.
When the page loads for the very first time, both OnItemCreated &
OnItemDataBound get fired & the Repeater with the 4 rows also gets
rendered. Next I click the first hyperlink (whose index is 0). When the
page re-loads, only OnItemCreated gets fired. Now although the entire
code in the Page_Load sub (which creates & opens a DB connection,
retrieves the records, fills the DataSet & finally spits out the
resultset in the Repeater control) is within the "If Not
(Page.IsPostBack)" condition, the Repeater still displays the 4 rows
when the page re-loads.
Now during this postback, the Repeater with the 4 rows gets recreated
from the ViewState, isn't it? This is what I did like to make sure of
i.e. whether the Repeater gets recreated from the ViewState or not?
Please correct me if I am wrong.
Thanks,
Arpan
it in a Repeater control:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
sqlConn = New SqlConnection("Data
Source=MyDS\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users",
sqlConn)
dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")
rptrUsers.DataSource = dSet
rptrUsers.DataMember = "Users"
rptrUsers.DataBind()
sqlConn.Close()
End If
End Sub
Sub BindData(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Data Bound<hr>")
End Sub
Sub ItemCreated(ByVal obj As Object, ByVal ea As
RepeaterItemEventArgs)
Response.Write("Item Created<br>")
End Sub
</script>
<form runat="server">
<asp:Repeater ID="rptrUsers" OnItemCreated="ItemCreated"
OnItemDataBound="BindData" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>NAME</th>
<th>PHONE NO.</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="linkbut" runat="server"><%#
Container.DataItem("UName") %></asp:LinkButton>
</td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td><%# Container.DataItem("UName") %></td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
Note that the records under the "NAME" column within the <ItemTemplate>
are hyperlinks whereas the records under the "NAME" column within the
<AlternatingItemTemplate> aren't hyperlinks. Assume that the Repeater
displays 4 rows.
When the page loads for the very first time, both OnItemCreated &
OnItemDataBound get fired & the Repeater with the 4 rows also gets
rendered. Next I click the first hyperlink (whose index is 0). When the
page re-loads, only OnItemCreated gets fired. Now although the entire
code in the Page_Load sub (which creates & opens a DB connection,
retrieves the records, fills the DataSet & finally spits out the
resultset in the Repeater control) is within the "If Not
(Page.IsPostBack)" condition, the Repeater still displays the 4 rows
when the page re-loads.
Now during this postback, the Repeater with the 4 rows gets recreated
from the ViewState, isn't it? This is what I did like to make sure of
i.e. whether the Repeater gets recreated from the ViewState or not?
Please correct me if I am wrong.
Thanks,
Arpan