将属性传递到 UserControl Asp.Net 时出现问题
我试图在 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 »</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>
任何帮助都会很棒,谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,您试图在(数据源的)初始化事件中访问视图状态(通过属性),但视图状态不可用,因此您的属性将返回空字符串。
您可以在加载事件而不是 init 中更改选择命令。请注意,您还可以使用 Page_Load 修改数据源属性。
另一种方法是处理 SqlDataSource.Selecting 事件并根据需要从事件参数修改 Command 对象(使用 SqlDataSourceCommandEventArgs.Command 用于访问命令选择事件处理程序)。
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).