Passing dropDownList SelectedValue by ControlParameter

Posted on 2007-08-01
Last Modified: 2008-01-09
It seems difficult to retrieve the data from a dropDownList when using UpdateParameters inside an ObjectDataSource. All I get in return is 'NULL'...

My example where I have a dropDownList which has its ListItems created by an init function in addition to a default listItem called "Default":
<asp:DropDownList OnInit="DropDownCreated" ID="ddlDropWhere" runat="server" Width="200">
     <asp:ListItem Text="Default" Value="" />

This should be passed by the objectDataSourc by its UpdateParameters:
     <asp:ControlParameter Name="dropWhere" controlID="ctl00$ContentPlaceHolder2$FormView1$ddlDropWhere" PropertyName="SelectedValue" Type="String" />

Further my BLL (which eventually saves the data to my database):
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool Save(String dropWhere, ...)

What have I missed?
(My project is ASP.NET C#.)
Question by:wahooo
    LVL 7

    Assisted Solution

    why are you using client id in your control parameters? can't you use the normal server side id of the dropdownlist?

    Author Comment

    The ControlID="ctl00$ContentPlaceHolder2$FormView1$ddlDropWhere" is a necessity to avoid this:
    Could not find control 'ddlDropWhere' in ControlParameter 'dropWhere'.

    Which is the result of setting ControlID="ddlDropWhere".
    LVL 16

    Accepted Solution

    Are you sure that values are being assigned to your list items? For example, in the above code, your "Default" item has a value of "" which is converted to NULL unless you set the ConvertEmptyStringToNull (not sure on exact name of this attribue off the top of my head but it's close to this) to false.

    Author Comment

    I have tested by sending the SelectedValue to a function in my aspx.cs-file, and it returns correct values according to my chosen item.

    This problem is really bugging me now, as I have tried for some hours (!!) to fix it.
    Any help and advice is highly appreciated.

    Author Comment

    It seems you are correct..
    I had put this out on hold in order to make some progress on my other work, and now I tested it again and the problem seems to be whatever item I select, the form/dropDownList only returns with the first item. I.e. the SelectedIndex is always 0, and therefore the SelectedValue is "" or "NULL" when it is passed on.

    How can I make my DropDownList pass on the item which is actually selected?

    Author Comment

    Thanks for all your help.
    In the end my initial code was correct and my fault actually lay hidden between somewhere:
    Inside my Page_Load(..) I did FormView1.DataBind(); which reset the edits done in the form and hence set the value of my selectedIndex to 0.

    Glad I found the error in the end though... :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now