【问题标题】:Passing parameter from gridview to sqldatasource error将参数从gridview传递到sqldatasource错误
【发布时间】:2012-05-22 19:10:10
【问题描述】:

我正在使用带有编辑/删除功能的 gridview。当用户单击“编辑”时,我需要将一个列 (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() 等数据绑定方法只能在数据绑定控件的上下文中使用。

来源错误:

[没有相关的源代码行]

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

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu parameter-passing


    【解决方案1】:

    我不认为你可以这样做。您可以在后面的代码中处理 RowEditing 事件并手动调整 SqlDataSource 以显示正确的数据。此外,将 SqlDataSoruce 移出网格。

    【讨论】:

    • 谢谢,我把它移到后面的代码中,并且能够解决参数传递问题。但现在我面临一个不同的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多