?
Solved

Changing a DataBound ListBox's SelectedValue INSIDE a Repeater WHILST Binding!

Posted on 2010-01-10
5
Medium Priority
?
779 Views
Last Modified: 2012-08-13
I'm struggling to get my head around this one...here goes...

I have 2 tables in my database: DETAILS and CATEGORIES
for each detail record there is a foreign key, "category_id" that relates to an id within "categories".

There is an unlimited number of detail records, so i have decided to use a repeater to dump them all to the screen in textboxes so the user can see and edit them. As it's a repeater my textboxes are created and populated on the databind. However my ListBox that displays the category for each detail record is also populated from a seperate binding.

The code below is working fine and shows how each of my textboxes and listboxes are populated. What i can't work out is once i've populated the category listbox "LbRptDetails" how can i set the "SelectedValue" of each one based on the database value "category_id" from my "details" table.

Not easy to explain, I can clarify if need be. Cheers
<table>
<asp:Repeater ID="RptDetails" runat="server">
   <ItemTemplate>
        <tr>
            <td class="tdLabel">
                 <asp:TextBox Width="180" MaxLength="100" ID="TbRptDetails_Title" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'  runat="server"/><br />
                 <asp:ListBox ID="LbRptDetails" Rows="1" runat="server"/>
            </td>
            <td>
                 <asp:TextBox ID="TbRptDetails_Content" Text='<%# DataBinder.Eval(Container.DataItem, "content") %>' TextMode="MultiLine" Rows="5" Width="500" MaxLength="1000"  runat="server"/>
            </td>
        </tr>
   </ItemTemplate>
</asp:Repeater>
</table>




Private Sub LoadDetails(ByVal IntBoatId As Integer)
        RptDetails.DataSource = 'Object Gets ALL details records from the database
        RptDetails.DataBind()
End Sub





Protected Sub RptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles RptDetails.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim ri As RepeaterItem = e.Item
            Dim LbRptDetails As ListBox = CType(ri.FindControl("LbRptDetails"), ListBox)

            
            Dim DtCategories As System.Data.DataTable
            DtCategories = 'Object Gets ALL category data from the Database
            LbRptDetails.DataSource = DtCategories
            LbRptDetails.DataTextField = "name"
            LbRptDetails.DataValueField = "id"
            LbRptDetails.DataBind()

        End If
    End Sub

Open in new window

0
Comment
Question by:ianmair329
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26279928
The line:

LbRptDetails.DataValueField = "id"

this is where the "Selected Value is stored. perhaps this should be:

LbRptDetails.DataValueField = "LbRptDetails.DataValueField = "category_id"

BTW: I would just cache the:

Dim DtCategories As System.Data.DataTable
            DtCategories = 'Object Gets ALL category data from the Database
            LbRptDetails.DataSource = DtCategories

some where so that we don't call the db and fetch the same data every time a item is set.
0
 
LVL 3

Author Comment

by:ianmair329
ID: 26281966
Hi and thanks for the second part of your comment. I had overlooked that I making multiple db calls.

Not quite sure what you're getting at for your solution to the problem though, could you clarify please
0
 
LVL 3

Author Comment

by:ianmair329
ID: 26281983
oh actually i just saw what you meant - "id" not "category_id". Don't worry about that i re-wrote my problem slightly to simplify for EE and didn't change that field name!

meanwhile I'm still not sure how to set a Selection for each of these listboxes as or when i've databinded them
0
 
LVL 3

Accepted Solution

by:
ianmair329 earned 0 total points
ID: 26288758
No further replies so I've solved my problem a different way:

I replaced the repeater with a placeholder, then created, named, data-bound and added the textbox & listbox controls in a loop.
0
 
LVL 3

Author Comment

by:ianmair329
ID: 26332930
as a comment also, if you use this method for inserting buttons or linkbuttons make sure you create an AddHandler for each button control. Also you may HAVE to give your controls an ID for them to work properly. hope that helps anyone :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question