Selecting Multiple Selection Listbox in a TemplateField?

Selecting Multiple Selection Listbox in a TemplateField?

am 30.01.2008 20:47:17 von 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:



DataSourceID="SqlRoleSource" DataTextField="RoleName"
DataValueField="RoleName" SelectionMode="Multiple">
Allow All




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

RE: Selecting Multiple Selection Listbox in a TemplateField?

am 31.01.2008 00:25:02 von rodoopus

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.



--
aaa


"Lance Wynn" wrote:

> 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:
>
>
>
> > DataSourceID="SqlRoleSource" DataTextField="RoleName"
> DataValueField="RoleName" SelectionMode="Multiple">
> Allow All
>

>

>

>
> 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
>
>
>