Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2010-01-10
5
Medium Priority
?
794 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
  • 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

572 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