将参数从网格视图传递到 sqldatasource 错误

本文关键字:sqldatasource 错误 视图 参数 网格 | 更新日期: 2023-09-27 18:35:04

我正在使用带有编辑/删除功能的网格视图。当用户单击"编辑"时,我需要将一个值从一列(100mPLot)传递到数据源,该数据源将填充另一列(SubPlot)的下拉列表。这是我所拥有的:

<asp:BoundField DataField="100mPlot" HeaderText="100m plot" 
     SortExpression="100mPlot" ReadOnly="True" />
<asp:TemplateField HeaderText="SubPlot" SortExpression="SubPlot">
    <EditItemTemplate>                            
        <asp:DropDownList ID="DropDownList1" runat="server" 
            DataSourceID="dsSubPlotNames" 
            DataTextField="SiteName" DataValueField="SiteID" 
            SelectedValue='<%# Bind("SiteID") %>'
        >
        </asp:DropDownList>
        <asp:SqlDataSource ID="dsSubPlotNames" runat="server" 
            ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand='exec [339_PPM].usp_SubPlotNames_Select null, @100mPlotName;'
            CancelSelectOnNullParameter="False">                                
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" DefaultValue='<%# Eval("100mPlot") '
                    Name="100mPlotName" PropertyName="SelectedValue" />
             </SelectParameters>
         </asp:SqlDataSource>
     </EditItemTemplate>
          <ItemTemplate>
              <asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot") %>'></asp:Label>
     </ItemTemplate>                        
 </asp:TemplateField>

不幸的是,我在 Eval("100mPlot") 中收到此错误:

数据绑定表达式仅在具有 DataBinding 事件的对象上受支持。System.Web.UI.WebControls.ControlParameter 没有 DataBinding 事件。

我该如何解决这个问题?谢谢

编辑:

我将其移动到代码隐藏中,并在创建包含上一列值的隐藏标签标签后在 RowDataBound 中处理它。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
     { 
         Label lbl100mPlot = (Label)e.Row.FindControl("lbl100mPlot");
         SqlDataSource dsPlotNames = (SqlDataSource)e.Row.FindControl("dsSubPlotNames");
         dsPlotNames.SelectParameters.Clear();
         dsPlotNames.SelectParameters.Add("100mPlotSiteID", null);
         dsPlotNames.SelectParameters.Add("100mPlotName", lbl100mPlot.Text);               
    }
}

我的下一个问题是这个错误:

数据绑定方法(如 Eval()、XPath() 和 Bind() 只能在数据绑定控件的上下文中使用。

异常详细信息:System.InvalidOperationException:Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。

源错误:

[无相关源行]

我不确定是什么原因造成的。

将参数从网格视图传递到 sqldatasource 错误

我认为你不能这样做。可以在代码隐藏中处理行编辑事件,并手动调整 SqlDataSource 以显示正确的数据。另外,将 SqlDataSoruce 移出网格。