在 ControlParameter 中找不到控件 ID

use*_*242 2 asp.net sqldatasource controlparameter sql-insert

我试图从文本框中插入值,但收到错误消息,无法在控制参数中找到 controlid。TextBox 位于表单视图内,而表单视图位于列表视图内。SqlDataSource 位于 ListView 之外。

我的 InsertButton_Click 方法

protected void InsertButton_Click(object sender, EventArgs e)
    {

        var ctrl = (Control)sender;
        var formview = (FormView)ctrl.NamingContainer;
        formview.ChangeMode(FormViewMode.Insert);
        SectionListView.DataSource = SectionDataSource;
        SectionListView.DataBind();

    }
Run Code Online (Sandbox Code Playgroud)

插入项目模板

<InsertItemTemplate>
    <tr style="">
        <td>
           <div style="font-size: .8em;">
               <asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
                   <ItemTemplate>
                       <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
                       <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
                       <asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
                       <asp:TextBox ID="SectionItemTextBox" runat="server" />
                       <asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
                       <asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>

                   </ItemTemplate>
               </asp:FormView>
           </div>
      </td>
  </tr>
</InsertItemTemplate>
Run Code Online (Sandbox Code Playgroud)

我的Sql数据源

<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
                    InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (@SectionItem, @SectionItemLabel)">
        <InsertParameters>
                <asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
                <asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
        </InsertParameters>
 </asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)

小智 5

当想要使用子控件作为控件参数时,需要使用限定的id作为控件id。尝试如下更改 SQL 数据源

<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
                    InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (@SectionItem, @SectionItemLabel)">
        <InsertParameters>
                <asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
                <asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
        </InsertParameters>
 </asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)