将属性传递到UserControl Asp.Net的问题

时间:2010-10-22 01:21:42

标签: asp.net vb.net repeater sqldatasource

我正在尝试在ASP.NET中创建一个UserControl来显示基于传入的两个值的新闻项,即NewsTag和ItemLimit。问题是SQLdatasource没有在SqlDataSource1_Init中获取属性。相反,在调用此属性时,对属性的调用为空,但在渲染后具有值。

<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>

任何帮助都会很棒,谢谢

1 个答案:

答案 0 :(得分:0)

这里的问题是你试图在init事件(数据源)中访问视图状态(通过属性),因为它不可用,所以你的属性将返回null字符串。

您可以在load事件而不是init中更改select命令。请注意,您还可以使用Page_Load修改数据源属性。

另一种方法是处理SqlDataSource.Selecting事件并根据需要从事件参数修改Command对象(在选择事件处理程序时使用SqlDataSourceCommandEventArgs.Command来访问命令)。