470,620 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

Using a DropDownList in a FormView with ObjectDataSource

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

Sep 14 '06 #1
4 12737
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


"J055" wrote:
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

Sep 14 '06 #2
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

"Mohamed Mosalem" <Mo************@discussions.microsoft.comwrote in
message news:85**********************************@microsof t.com...
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


"J055" wrote:
>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


Sep 14 '06 #3
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("ddlEdit Mode")).SelectedValue ;
}
"J055" wrote:
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

"Mohamed Mosalem" <Mo************@discussions.microsoft.comwrote in
message news:85**********************************@microsof t.com...
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


"J055" wrote:
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



Sep 14 '06 #4
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Sep 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by FR | last post: by
1 post views Thread by Chris | last post: by
reply views Thread by rodchar | last post: by
reply views Thread by kidders | last post: by
reply views Thread by jphaycock | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.