简体   繁体   English

在formview中使用下拉列表控件

[英]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. 我是asp.net的新手,我在使用formview中的dropdownlist控件并将其值传递给相关的sqldatasource时遇到问题。 When i use the code below i get the following exception 当我使用下面的代码时,我得到以下异常

Exception Details: System.InvalidOperationException: Could not find control 'ddlCategory' in ControlParameter 'categoryId'. 异常详细信息:System.InvalidOperationException:无法在ControlParameter“categoryId”中找到控件“ddlCategory”。

The Dropdownlist inside a formview. formview中的Dropdownlist。

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

The SQL Data Source SQL数据源

     <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. 我在控制参数中更改了DDL的ID。 It works like below since this is the final generated id of that control. 它的工作原理如下,因为这是该控件的最终生成ID。 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" />

It is because of your ddlCategory is inside formview and you are using the master page. 这是因为您的ddlCategory位于formview中,并且您正在使用母版页。 The best way is to overwrite the 'FindControl' function of master page. 最好的方法是覆盖母版页的'FindControl'功能。 pls see the following link for detail: 请参阅以下链接了解详细信息:

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

This answer will provide a solution to your problem: 这个答案将为您的问题提供解决方案:

You need a recursive findcontrol() method. 您需要一个递归的findcontrol()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM