DataBinding DropDownList Selected Value In A FormView

Posts   
 
    
Phuzakie
User
Posts: 13
Joined: 10-Nov-2006
# Posted on: 16-Nov-2006 16:36:41   

I'm looking to see if anyone has solved the problem that I am having already.

I use a formview connected to a llblgenprodatasource. If I use <%# Bind() %> in the .aspx page for my text box, the value of the text box is saved.

Since a dropdownlist has no place to <%# Bind() %> the selected value, is there anyway to connect the selected value to the datasource in the code-behind? The goal is to save the selected value.

I have my .aspx page:


<llblgenpro:llblgenprodatasource id="dsInspection" runat="server" datacontainertype="EntityCollection" entitycollectiontypename="VISFW.CollectionClasses.InspectionCollection, VISFW" cachelocation="Session" />

<asp:formview id="fvInspection" runat="server" defaultmode="ReadOnly" datasourceid="dsInspection" datakeynames="ID" width="100%">

<itemtemplate>
    <table width="100%">
        <tr>
            <td width="25%">ID</td>
            <td width="25%">Mileage</td>
            <td width="25%">Service Type</td>
            <td width="25%">&nbsp;</td>                     
        </tr>
        <tr>
            <td width="25%"><asp:label id="lblID" runat="server" text='<%# Eval("ID") % >' /></td>
            <td width="25%"><asp:label id="lblDate" runat="server" text='<%# Eval("Mileage") %>' /></td>
            <td width="25%"><asp:label id="lblServiceType" runat="server"/></td></td>
            <td width="25%">&nbsp;</td>
        </tr>
    </table>
</itemtemplate>

<edititemtemplate>
    <table width="100%">
        <tr>
            <td width="25%">ID</td>
            <td width="25%">Mileage</td>
            <td width="25%">Service Type</td>
            <td width="25%">&nbsp;</td>                 
        </tr>
        <tr>
            <td width="25%"><asp:label id="lblID" runat="server" text='<%# Eval("ID") %>' /></td>
            <td width="25%"><asp:textbox id="editDate" runat="server" text='<%# Bind("Mileage") %>' /></td>
            <td width="25%">asp:dropdownlist id="ddlServiceType" runat="server" /></td>
            <td width="25%">&nbsp;</td>
        </tr>                   
    </table>
</edititemtemplate>

</asp:formview>

and my VB code-behind:


Protected Sub fvInspection_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvInspection.DataBound

Dim CurrentInspection As InspectionEntity = fvInspection.DataItem

Select Case fvInspection.CurrentMode
    Case FormViewMode.ReadOnly
        'Bind Service Type
            Dim refServiceType As New InspectionServiceTypeEntity
            refServiceType.FetchUsingPK(CurrentInspection.InspectionServiceID)
            Dim lblServiceType As Label = fvInspection.FindControl("lblServiceType")
            lblServiceType.Text = refServiceType.Description

    Case FormViewMode.Edit
        'Bind Service Type
            Dim refServiceType As New InspectionServiceTypeCollection
            refServiceType.GetMulti(Nothing)
            Dim ddlServiceType As DropDownList = fvInspection.FindControl("ddlServiceType")
            ddlServiceType.DataSource = refServiceType
            ddlServiceType.DataTextField = "Description"
            ddlServiceType.DataValueField = "ID"
            ddlServiceType.DataBind()               
            ddlServiceType.Items.FindByValue(CurrentInspection.InspectionServiceID).Selected = True

End Select

End Sub

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Nov-2006 08:29:43   

Since a dropdownlist has no place to <%# Bind() %> the selected value

There is a place, you need to do the following:

<asp:dropdownlist id="ddlServiceType" runat="server" SelectedValue='<%# Bind("YourField")%>' />
Phuzakie
User
Posts: 13
Joined: 10-Nov-2006
# Posted on: 17-Nov-2006 18:25:25   

Walaa wrote:

There is a place, you need to do the following:


<asp:dropdownlist id="ddlServiceType" runat="server" SelectedValue='<%# Bind("YourField")%>' />

OK... If I change my .aspx to use...


<asp:dropdownlist id="ddlServiceType" runat="server" SelectedValue='<%# Bind("ServiceTypeID")%>' />

I get the following .NET error...

'ddlServiceType' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

This is because I currently fill my drop down list when my FormView is DataBound and it tries to bind it before it's getting filled.

So I moved my code to fill my drop down, to a protected sub, called when that drop down is initialized.


<asp:dropdownlist id="ddlServiceType" runat="server" oninit="ddlServiceType_onInit" SelectedValue='<%# Bind("ServiceTypeID")%>' />


Protected Sub ddlServiceType_onInit(ByVal sender As Object, ByVal e As System.EventArgs)
    'Bind Service Type
        Dim refServiceType As New InspectionServiceTypeCollection
        refServiceType.GetMulti(Nothing)
        Dim ddlServiceType As DropDownList = CType(sender, DropDownList)
        ddlServiceType.DataSource = refServiceType
        ddlServiceType.DataTextField = "Description"
        ddlServiceType.DataValueField = "ID"
        ddlServiceType.DataBind()
End Sub

It fires the onInit, but the binding of the list values seems to trigger the Bind() in the aspx page. Now I get the following error...

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

The bottom line is that I want to fill a drop down list from my Service Type Collection and then bind the selected value to the Inspection Entity being retrieved.

Phuzakie
User
Posts: 13
Joined: 10-Nov-2006
# Posted on: 17-Nov-2006 18:36:41   

If I change the onInit code to this...


Protected Sub ddlServiceType_onInit(ByVal sender As Object, ByVal e As System.EventArgs)
    'Bind Service Type
        Dim ddlServiceType As DropDownList = CType(sender, DropDownList)
        Dim NewListItem As ListItem
        Dim CurrentServiceType As InspectionServiceTypeEntity
        Dim CurrentServiceTypes As New InspectionServiceTypeCollection
        CurrentServiceTypes.GetMulti(Nothing)
        CurrentServiceTypes.Sort(InspectionServiceTypeFieldIndex.Description, ListSortDirection.Ascending) 
        For Each CurrentServiceType In CurrentServiceTypes
            NewListItem = New ListItem
            NewListItem.Text = CurrentServiceType.Description
            NewListItem.Value = CurrentServiceType.ID
            ddlServiceType.Items.Add(NewListItem)
        Next
End Sub

It comes down to not being able actually binding the list to fill it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 17-Nov-2006 19:09:33   

Is the name perhaps ServiceTypeId ?

Frans Bouma | Lead developer LLBLGen Pro
Phuzakie
User
Posts: 13
Joined: 10-Nov-2006
# Posted on: 17-Nov-2006 20:28:37   

Otis wrote:

Is the name perhaps ServiceTypeId?

No, I turned off pascal casing and my IDs are named with ID.

My last post fixed the problem. It just comes down to not being able to Databind a drop down list twice. I don't fill the list with a databind anymore, I simply loop thru and add items. The only thing actually bound now is the selected value.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 17-Nov-2006 20:44:00   

You can bind a collection twice, you've to do it this way: when you bind a collection to a control, its DefaultView object is bound to it. If you bind that same collection to another control, again that DefaultView object is bound to it instead. However, that's the same object, so it doesn't act as a different object.

If you create a new EntityView object from the collection, and bind that instead, it should work.

Frans Bouma | Lead developer LLBLGen Pro