4

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" />
rematnarab
  • 1,277
  • 4
  • 22
  • 42

2 Answers2

0

It is because of your ddlCategory is inside formview and you are using the master page. The best way is to overwrite the 'FindControl' function of master page. pls see the following link for detail:

http://geekswithblogs.net/AzamSharp/archive/2006/08/27/89475.aspx

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
ayhtut
  • 1
0

This answer will provide a solution to your problem:

You need a recursive findcontrol() method.

Community
  • 1
  • 1
EJC
  • 2,062
  • 5
  • 21
  • 33