Using a DropDownList in a FormView with ObjectDataSource

Discussion in 'ASP .Net' started by J055, Sep 14, 2006.

  1. J055

    J055 Guest

    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
     
    J055, Sep 14, 2006
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
     
    =?Utf-8?B?TW9oYW1lZCBNb3NhbGVt?=, Sep 14, 2006
    #2
    1. Advertising

  3. J055

    J055 Guest

    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" <> wrote in
    message news:...
    > 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
    >>
    >>
    >>
    >>
     
    J055, Sep 14, 2006
    #3
  4. 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 ;
    }


    "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" <> wrote in
    > message news:...
    > > 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
    > >>
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?TW9oYW1lZCBNb3NhbGVt?=, Sep 14, 2006
    #4
  5. 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 (, 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.
     
    Walter Wang [MSFT], Sep 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9obiBCYWlsZXk=?=

    FormView and ObjectDataSource - Delete doesn't work

    =?Utf-8?B?Sm9obiBCYWlsZXk=?=, Jun 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    4,363
    =?Utf-8?B?Sm9obiBCYWlsZXk=?=
    Jun 3, 2005
  2. =?Utf-8?B?RGFiYmxlcg==?=

    field order with ObjectDataSource in FormView or DataView

    =?Utf-8?B?RGFiYmxlcg==?=, Apr 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    551
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 5, 2006
  3. =?Utf-8?B?Z292ZXI=?=

    FileUpload Control in FormView using ObjectDataSource

    =?Utf-8?B?Z292ZXI=?=, Sep 2, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    732
    =?Utf-8?B?Z292ZXI=?=
    Sep 2, 2007
  4. David Thielen

    ObjectDataSource method as another ObjectDataSource

    David Thielen, Mar 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    245
    Steven Cheng[MSFT]
    Mar 23, 2006
  5. Lagrange
    Replies:
    1
    Views:
    567
    Lagrange
    Jan 17, 2009
Loading...

Share This Page