Selecting Multiple Selection Listbox in a TemplateField?

L

Lance Wynn

Hi all,
I have been working on this seeming simple problem the better part of the
morning, and can't seem to figure it out.

I have a listbox as a template:

<asp:TemplateField HeaderText="Roles" SortExpression="Roles">
<EditItemTemplate>
<asp:ListBox ID="ListBox1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlRoleSource" DataTextField="RoleName"
DataValueField="RoleName" SelectionMode="Multiple">
<asp:ListItem Value="*">Allow All</asp:ListItem>
</asp:ListBox>
</EditItemTemplate>
</asp:TemplateField>

It is bound to a Datasource that queries all the Roles from my application
security. When editing, I want it to highlight all the Roles that the user
belongs to.

I was experimenting with something like the following, but I can never
access anything but the ListItems that are hard coded into the template,
none of the bound items seem to be accessible:

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound
If DetailsView1.CurrentMode = DetailsViewMode.Edit Or
DetailsView1.CurrentMode = DetailsViewMode.Insert Then
Dim listbox1 As ListBox = DetailsView1.FindControl("ListBox1")
For Each item As ListItem In listbox1.Items
'Logic for selecting item here if the user belongs to the role.
Next
End If
End Sub

Can anyone tell me what I am missing please?

Thank You
Lance
 
A

Angel

Not in a million years the way you are trying to use it

For DetailsView control plain databoundfield

dim mytext as string
mytext = Ctype(dv.Rows(1).Cells(1).Controls(0), TextBox).Text

For Template field (withing detailsview)

dim ph as PlaceHolder
ph = Ctype(dv.Rows(2).Cells(1).Controls(0), PlaceHolder)
temp = Ctype(ph.FindControl("ddlSupplierID"), DropDownList).SelectedValue

The placeholder may or may not be there. Depends on how you created the
fields to begin with.

Key-- 1) You need to be in the right collection to find the control.
2) Make sure you requesting the correct id value case matters so
ddlSupplierID is not the same as ddlsupplierid

But remember just say dv.findcontrol(... will not work because the function
will not traverse objects within the control. This little fact ate weeks out
of my life.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top