I think SqlDataSource is not a binding control, so you cannot bind data to it in the ListView. You can place a SqlDataSource into ItemTemplate of ListView without any binding to it. Then handle the ItemDataBound event of ListView to set value to it like
this:
It is better not to bind ID. Just SqlDataSource2 will do. ASP.NET only requires the ID to be unique within the NamingContainer. (In this case, the ListViewDataItem.)
Also, if you use HiddenField, like Qin Dian Tang suggests, you can use ControlParameter instead of Parameter. That will pick up the value of the HiddenField automatically, making it work without code-behind.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
The solution is combining the code from the original post with what Qin Dian Tang posted. Do you really need someone else to do that for you?
Once you have that done, putting in a HiddenField, as I suggested is kind of trivial. So is changing the Parameter to a ControlParameter and making it refer to the HiddenField.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
ondra
Member
54 Points
167 Posts
Using repeater in Listview
Feb 21, 2012 08:46 PM|LINK
Hi,
i need to use repeater in listview. I tried this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [projects]"> </asp:SqlDataSource> <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"><ItemTemplate> <script type="text/javascript"> $(function() { $('#gallery<%# Eval("gallery") %> a').lightBox(); }); </script> <span ID='gallery<%# Eval("gallery") %>'><span class="projectname"> <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /> </span> <br /> <asp:SqlDataSource ID='SqlDataSource2_<%# Eval("gallery") %>' runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [galleries] WHERE ([ID] = '<%# Eval("gallery") %>')"> </asp:SqlDataSource> <asp:Repeater ID="Repeater1" runat="server" DataSourceID='SqlDataSource2_<%# Eval("gallery") %>'> anc </asp:Repeater> <asp:Image ID="picturebox" runat="server" ImageUrl='<%# Eval("picture") %>' /> <br /> </span> </ItemTemplate> <LayoutTemplate> <div ID="itemPlaceholderContainer" runat="server" style=""> <span ID="itemPlaceholder" runat="server" /> </div> <div style=""> <asp:DataPager ID="DataPager1" runat="server" PageSize="2"> <Fields> <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="False" ShowNextPageButton="False" ShowPreviousPageButton="False" /> <asp:NumericPagerField /> <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="False" ShowNextPageButton="False" ShowPreviousPageButton="False" /> </Fields> </asp:DataPager> </div> </LayoutTemplate> </asp:ListView>but it is not working, any idea whats wrong ?
thanks,Ondra
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Using repeater in Listview
Feb 23, 2012 07:52 AM|LINK
Hi,
I think SqlDataSource is not a binding control, so you cannot bind data to it in the ListView. You can place a SqlDataSource into ItemTemplate of ListView without any binding to it. Then handle the ItemDataBound event of ListView to set value to it like this:
Add this to ListView:
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("gallery") %>' />
SelectCommand="SELECT * FROM [galleries] WHERE ([ID] = @ID)"
<SelectParameters>
<asp:Parameter Name="ID" />
</SelectParameters>
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
SqlDataSource sql = (SqlDataSource)e.Item.FindControl("SqlDataSource2");
sql.SelectParameters["ID"].DefaultValue = ((HiddenField)e.Item.FindControl("HiddenField1")).Value;
}
}
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
superguppie
All-Star
48225 Points
8679 Posts
Re: Using repeater in Listview
Feb 24, 2012 12:33 PM|LINK
It is better not to bind ID. Just SqlDataSource2 will do. ASP.NET only requires the ID to be unique within the NamingContainer. (In this case, the ListViewDataItem.)
Also, if you use HiddenField, like Qin Dian Tang suggests, you can use ControlParameter instead of Parameter. That will pick up the value of the HiddenField automatically, making it work without code-behind.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
warrenkc2
Member
71 Points
75 Posts
Re: Using repeater in Listview
Mar 05, 2012 01:22 AM|LINK
Is it possible to see the answer by showing sample code? I am not sure what is meant by the two posts. I would really appreciate it.
superguppie
All-Star
48225 Points
8679 Posts
Re: Using repeater in Listview
Mar 06, 2012 01:06 PM|LINK
The solution is combining the code from the original post with what Qin Dian Tang posted. Do you really need someone else to do that for you?
Once you have that done, putting in a HiddenField, as I suggested is kind of trivial. So is changing the Parameter to a ControlParameter and making it refer to the HiddenField.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
warrenkc2
Member
71 Points
75 Posts
Re: Using repeater in Listview
Mar 06, 2012 02:40 PM|LINK
Thank you. I just did not understand.
ondra
Member
54 Points
167 Posts
Re: Using repeater in Listview
Mar 12, 2012 09:07 PM|LINK
thanks :)