Search code examples
asp.netsqldatasourcecontrolparameter

Using a dropdownlist control inside a formview


I am new to asp.net and i have a problem using a dropdownlist control inside a formview and passing its value to the related sqldatasource. When i use the code below i get the following exception

Exception Details: System.InvalidOperationException: Could not find control 'ddlCategory' in ControlParameter 'categoryId'.

The Dropdownlist inside a formview.

      <asp:DropDownList ID="ddlCategory" DataSourceID="ObjectDataSourceCategory" DataTextField="NAME" DataValueField="ID" runat="server" />

The SQL Data Source

     <asp:ObjectDataSource ID="sqlDataSourceItem" TypeName="Item" runat="server"
      SelectMethod="getItem"
      InsertMethod="insertItem"
      UpdateMethod="updateItem">
     <SelectParameters>
        <asp:QueryStringParameter QueryStringField="id" Name="id" />
     </SelectParameters>
     <InsertParameters>
        <asp:ControlParameter ControlID="ddlCategory" Name="categoryId" PropertyName="selectedValue" />
     </InsertParameters>
     </asp:ObjectDataSource>

And i have found the solution to this problem. I have changed the ID of the DDL in the control parameter. It works like below since this is the final generated id of that control. But i think there must be an easier and better way. Any help would be appriciated.

    <asp:ControlParameter ControlID="ctl00$main$frmViewItem$ddlCategory" Name="categoryId" PropertyName="selectedValue" />

Solution

  • This answer will provide a solution to your problem:

    You need a recursive findcontrol() method.