1
我試圖在ASP.NET中創建一個UserControl來顯示基於傳入的兩個值的新聞項,NewsTag和ItemLimit。問題是SQLdatasource沒有拾取SqlDataSource1_Init中的屬性。相反,當調用這個屬性時,對這些屬性的調用是空的,但是在渲染具有值之後。將屬性傳遞到UserControl的問題Asp.Net
<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>
任何幫助將是巨大的,感謝
這裏
我已經嘗試了兩種建議,並且在控件嘗試綁定之前正在更改SQLcommand,但從參數傳遞的值爲null。這會導致錯誤的語法錯誤。 – 2010-10-22 17:04:30
@Mikey,如果屬性返回NULL意味着沒有視圖狀態。作爲用戶控制內的邏輯,它可以與此相關。你是否動態加載用戶控件?你能分享在頁面上使用用戶控件的代碼嗎?無論如何,你需要確保視圖狀態的兩件事 - 確保將相同的ID分配給用戶控件,並儘可能早地加載用戶控件(設計時間最好,但下一個最佳選擇是Page_Init等) 。 – VinayC 2010-10-25 05:45:47