I'm trying to create a UserControl in ASP.NET to display news items based on two values that are passed in, NewsTag and ItemLimit. The problem is that the SQLdatasource is not picking up the properties in SqlDataSource1_Init. Instead the calls to the properties are empty when this is called but after render have values.
<script runat="server">
Public Property NewsTag() As String
Get
Dim o As Object = ViewState("NewsTag")
If Not o Is Nothing Then Return CStr(o) Else Return Nothing
End Get
Set(ByVal value As String)
ViewState("NewsTag") = value.Replace(" ", "")
End Set
End Property
Public Property ItemLimit() As String
Get
Dim o As Object = ViewState("ItemLimit")
If Not o Is Nothing Then Return CStr(0) Else Return Nothing
End Get
Set(ByVal value As String)
'Limit news items to 50, for readability
ViewState("ItemLimit") = Math.Max(Math.Min(CInt(value), 50), 5)
End Set
End Property
Protected Sub SqlDataSource1_Init(ByVal sender As Object, ByVal e As System.EventArgs)
SqlDataSource1.SelectCommand = "SELECT top " + ItemLimit() + " a.[id], a.[itemdate], a.[title], a.[description], a.[photo] FROM [Announcements] a, [NewsInTags] n, [Tags] t WHERE ('" + NewsTag() + "' = t.Tag AND t.id = n.TagId AND a.id = n.NewsId) OR (n.TagId = 1 AND a.id = n.NewsId) order by itemdate desc "
SqlDataSource1.DataBind()
End Sub
</script>
<div class="halflist">
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div class="listitem">
<h3>
<a href='<%# "/News/Article/" &Cstr( Eval("ID"))& "/" & helpers.urlSafe(Cstr( Eval("title")))%>'>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("title") %>' />
</a>
</h3>
<div class="thumbnail">
<a href='<%# "/News/Article/" &Cstr( Eval("ID"))& "/" & helpers.urlSafe(Cstr( Eval("title")))%>'>
<Club:IThumb ID="ImageThumbnail2" runat="server" PhotoID='<%# Eval("photo") %>' />
</a>
</div>
<asp:Label ID="Label3" class="liststamp" Font-Size="Smaller" runat="server" Text='<%# Eval("itemdate","{0:d}") %>' />
<p>
<asp:Label ID="Label2" runat="server" Text='<%# helpers.CutIt(Convert.ToString(Eval("description")),150)%>' />
<a class="readMore" href='<%# "/News/Article/" &Cstr( Eval("ID"))& "/" & helpers.urlSafe(Cstr( Eval("title")))%>'>read more »</a>
</p>
<div class="clearlist">
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
SelectCommand="SELECT top @items a.[id], a.[itemdate], a.[title], a.[description], a.[photo] FROM [Announcements] a, [NewsInTags] n, [Tags] t WHERE (@tag = t.Tag AND t.id = n.TagId AND a.id = n.NewsId) OR (n.TagId = 1 AND a.id = n.NewsId) order by itemdate desc "
oninit="SqlDataSource1_Init">
<SelectParameters>
<asp:Parameter Name="items" Type="Int16" DefaultValue="5" />
<asp:Parameter Name="tag" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Any help would be great, thanks