无法在ControlParameter中找到控件ID

时间:2014-08-19 14:29:39

标签: asp.net sql-insert sqldatasource controlparameter

我正在尝试从文本框中插入值,但是我收到一个错误,它无法在controlparameter中找到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();

    }

InsertItem模板

<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>

我的SqlDataSource

<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>

1 个答案:

答案 0 :(得分:4)

如果要将子控件用作控件参数,则需要使用限定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>