【问题标题】:Update the database from SqlDataAdapter using UpdateParameters使用 UpdateParameters 从 SqlDataAdapter 更新数据库
【发布时间】:2014-03-28 19:17:03
【问题描述】:

我有一个问题需要帮助,我有一个来自 SqlDataSource 的 gridview 绑定:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource2">
    <Columns>
        <asp:CommandField ShowEditButton="true" />
        <asp:BoundField DataField="DateOpened" HeaderText="DateOpened" 
            SortExpression="DateOpened" />
        <asp:BoundField DataField="rName" HeaderText="rName" SortExpression="rName" />
        <asp:BoundField DataField="DateOfArrival" HeaderText="DateOfArrival" 
            SortExpression="DateOfArrival"/>
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" 
            SortExpression="Quantity"/>
        <asp:BoundField DataField="sName" HeaderText="sName" SortExpression="sName" />
        <asp:TemplateField HeaderText="osName" SortExpression="osName">
            <EditItemTemplate>
                <asp:DropDownList runat="server" ID="OrderStatusDDL">
                <asp:ListItem>הוזמן</asp:ListItem>
                <asp:ListItem>מתעכב</asp:ListItem>
                <asp:ListItem>התקבל</asp:ListItem>
                </asp:DropDownList> 
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("osName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:igroup9_test1ConnectionString %>" 
    SelectCommand="spGetOrdersListForProject" 
    SelectCommandType="StoredProcedure" UpdateCommandType="StoredProcedure" UpdateCommand="spUpdateOrderStatus" >
    <SelectParameters>
        <asp:ControlParameter ControlID="ProjectIDHolder" DefaultValue="" 
            Name="ProjectID" PropertyName="Text" Type="Int32" />
    </SelectParameters>

    <UpdateParameters>
       <asp:ControlParameter ControlID="ProjectIDHolder" DefaultValue="" 
               Name="@projectid" PropertyName="Text" Type="Int32" />
       <asp:ControlParameter Name="@osID"  PropertyName="Text" Type="Int32"  
               ControlID="OrderStatusDDL" />
    </UpdateParameters>
</asp:SqlDataSource>

我想编辑osName 列,当我按下Edit 按钮并尝试从下拉列表中选择不同的值时,我想更新数据库中的osId (int) 但我得到这个错误:

在 ControlParameter '@osID' 中找不到控件 'OrderStatusDDL'。

【问题讨论】:

    标签: asp.net gridview sqldatasource


    【解决方案1】:

    GridView 是否在使用 MasterPage 的页面上?如果是这样,您需要在控件前面加上 ContentPlaceHolder 的 ID。

    <asp:ControlParameter Name="@osID"  PropertyName="Text" Type="Int32"
    ControlID="ContentPlaceholderID$OrderStatusDDL" />
    

    【讨论】:

    • 查看渲染的HTML,看看控件的ID是什么。
    • 控件 id 是:ContentPlaceHolder3_GridView1_OrderStatusDDL_0”,当我尝试使用该 id 时,错误是“在 ControlParameter '@projectid' 中找不到控件'ContentPlaceHolder3_GridView1_OrderStatusDDL_0'。”
    • 错误信息现在是 @projectid 而不是 @osID?
    【解决方案2】:

    您似乎正试图将 OrderStatusDDL.SelectedValue 的文本值传递到一个 int 字段中。尝试将类型更改为字符串,看看是否有帮助。

    <asp:ControlParameter Name="@osID"  PropertyName="Text" Type="String" ControlID="OrderStatusDDL" />
    

    我不知道这是否会有所帮助,但这可能是一个不错的起点。

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多