Using a DropDownList in a FormView with ObjectDataSource

J

J055

Hi

I thought I was trying to do something very simple but I'm have a lot of
trouble trying to do the following.

<asp:FormView ID="fvGroups" runat="server" DataKeyNames="GroupID"
DataSourceID="odsGroups"
DefaultMode="Insert" Visible="false" OnItemCommand="fvGroups_ItemCommand">
<InsertItemTemplate>
<asp:DropDownList ID="ddlEditMode" runat="server"
OnInit="ddlEditMode_Init" SelectedValue='<%# Bind("EditMode") %>'>
<asp:ListItem Selected="True" Value="0" Text="Select..." ></asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>

....

protected void ddlEditMode_Init(object sender, EventArgs e)
{
DropDownList ddlEditMode = sender as DropDownList;
ddlEditMode.EnableViewState = false;
ddlEditMode.AppendDataBoundItems = true;
ddlEditMode.DataSource = myList; // an OrderedDictionary object
ddlEditMode.DataTextField = "value";
ddlEditMode.DataValueField = "key";
// Bind the data to the control.
ddlEditMode.DataBind();
}

When I add SelectedValue='<%# Bind("EditMode") %>'> to the DroupDownList I
get this error.

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

I just want to save the value to the ObjectDataSource InsertParameters.
Surely this is a very standard requirement?

Thanks
Andrew
 
G

Guest

Hi,
In the insert mode you won't need to use <%# Bind("") %> because you are
inserting new item so no data to bind to.
all you have to do is to configure the data source to have a control
parameter in its insert parameters that points to the drop down list.

Hope this helps.
Regards,
Mohamed Mosalem
 
J

J055

Hi Mohamed

I thought a ControlParameter would work but if I add one to the
ObjectDataSource with 'ddlEditMode' as the ControlID I get a 'can't find the
Control' error.

Any ideas?

Thanks
Andrew
 
G

Guest

Try the following code in the ItemInserting event handler of the form view
protected void fvGroups_ItemInserting(object sender, FormViewInsertEventArgs
e)
{

e.Values["EditMode"]=((DropDownList)FormView1.Row.FindControl("ddlEditMode")).SelectedValue ;
}
 
W

Walter Wang [MSFT]

Hi Andrew,

A control added in an item template will have to be referenced using
FindControl just as Mohamed showed in his code. That's why ObjectDataSource
will complain about can't find the control.

I think Mohamed's suggestion will work for you; please let us know your
result so that we can further follow up. Thank you.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top