views:

17

answers:

1

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 &raquo;</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

A: 

Issue here is that you are trying to access the view-state (via properties) in init event (of data-source) where it is not available so your properties will return null string.

You can change your select command in load event instead of init. Note that you can also use Page_Load to modify your data source properties.

Yet another way would be to handle SqlDataSource.Selecting event and modify the Command object from event arguments as per your need (use SqlDataSourceCommandEventArgs.Command for accessing the command in selecting event handler).

VinayC
I have tried both of your suggestions and the SQLcommand is being changed before the control tries to bind but the values that are being passed from the parameter are null. This causes a Incorrect Syntax error.
Mikey
@Mikey, if properties are returning NULL means no view-state. As your logic in within user-control, it can be related to that. Are you loading the user control dynamically? Can you share the code that uses the user control on page? Regardless, you need to ensure two things for view-state to work - ensure that the same ID gets assigned to the user control and user-control gets loaded as early as possible (design time is best but next best choice would be Page_Init etc).
VinayC