JoonasL
JoonasL

Reputation: 524

Getting database ID of objects in ListView and updating ListView from user control ListView

I am creating a website and am facing the following problem. I have 2 ListViews. The first ListView is inside a user control called Sidebar.ascx:

<asp:ListView ID="sidebarListView" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSourceSidebar">
 <ItemTemplate>
  <div class="sidebarItem" runat="server">
     <div>
        <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
     </div>
  </div>
 </ItemTemplate>
 <LayoutTemplate>
  <div class="sidebarMain">
     <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
  </div>
 </LayoutTemplate>
</asp:ListView>

<asp:SqlDataSource ID="SqlDataSourceSidebar" runat="server" ConnectionString="<%$ ConnectionStrings:TudengiDBConnectionString %>" SelectCommand="SELECT [Id], [Name] FROM [Faculties] ORDER BY [Name]"></asp:SqlDataSource>

It has to display only the name.

The second listview is inside my Default.aspx

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
  <ContentTemplate>
     <asp:ListView ID="RecentItemsListView" runat="server" DataSourceID="SqlDataSource1"
        GroupItemCount="3">
        <LayoutTemplate>
           <div class="recentItemsMain">
              <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
           </div>
           <asp:DataPager ClientIDMode="Static" ID="DataPager1" runat="server" PageSize="9">
              <Fields>
                 <asp:NumericPagerField />
              </Fields>
           </asp:DataPager>
        </LayoutTemplate>
        <GroupTemplate>
           <div class="recentItems">
              <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
           </div>
        </GroupTemplate>
        <ItemTemplate>
           <div class="recentItem" runat="server">
              <div>
                 <asp:Image ID="PictureThumb" runat="server" ImageUrl='<%#CreateThumbnail((string)Eval("Picture"),130,130) %>' />
              </div>
              <asp:Label ID="AuthorLabel" runat="server" Text='<%# Eval("Author") %>' />
              <div>
                 <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
              </div>
           </div>
        </ItemTemplate>
        <GroupSeparatorTemplate>
           <div class="groupSeparator">
           </div>
        </GroupSeparatorTemplate>
     </asp:ListView>

  </ContentTemplate>
</asp:UpdatePanel>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TudengiDBConnectionString %>" SelectCommand="SELECT [Id], [Name], [Faculty_Id], [User_Id], [Author], [Picture], [Location] FROM [Books] ORDER BY [DateAdded] DESC">

What I need is for the ListView in Default.aspx to display the data without a WHERE clause, but when an item is clicked in the Sidebar user control I need to update the Default.aspx ListView to display only the data where the [Faculty_Id] = the ID of the ListView item in the user control.

How can I get the database ID of the ListView object when I can only display the NAME field? Do I have to display the ID as well and then hide the column from users? What is the correct way to solve a situation like this?

Thanks for helping

Upvotes: 2

Views: 841

Answers (3)

JoonasL
JoonasL

Reputation: 524

<asp:ListView ID="sidebarListView" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSourceSidebar" OnItemCommand="sidebarListView_ItemCommand">
<ItemTemplate>
  <div class="sidebarItem" runat="server">
     <div>
        <asp:LinkButton ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' CommandName="Select" CommandArgument='<%# Eval("Id") %>' />
     </div>
  </div>
</ItemTemplate>

This is what I ended up with.

Upvotes: 1

Boomer
Boomer

Reputation: 1478

What you can do is to add an attribute to the NameLabel control. Call it for example myID:

<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' myID='<%# Eval("ID") %>'/>

then access that attribute from your code to get your ID:

string ID = NameLabel.Attributes["myID"];

Upvotes: 0

Channs
Channs

Reputation: 2101

Add a hidden-field to your item-template to hold the database ID.

<ItemTemplate>
   <asp:HiddenField ID="hdnFacultyID" runat="server" Value='<%# Eval("FacultyID") %>' />
   <div class="recentItem" runat="server">
   ...
   </div>
 </ItemTemplate>

In the appropriate ListView handlers, you can access the database ID something like this.

((HiddenField)e.item.FindControl("hdnFacultyID")).Value

Upvotes: 0

Related Questions