将参数传递给绑定到 gridview 更新中的下拉列表的 sqldatasource

本文关键字:下拉列表 sqldatasource 更新 gridview 参数传递 绑定 | 更新日期: 2023-09-27 18:34:42

我有一个包含许多列的可编辑网格视图。其中两个是 100mPlot 和 SubPlot。子图不是唯一的(1A、1B、1C 和 1D(,但 100mPlot + 子图构成了一组唯一的值。

当用户单击"编辑"时,他们将在"子绘图"列中看到一个下拉列表。此列表需要基于 100mPlot 的值。因此,为了显示子图的正确值,我需要将 100mPlot 列中的值作为参数传递给将绑定到子图下拉列表的 sqldatasource。我该怎么做?

<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="SiteID" DataValueField="SiteID" 
             SelectedValue='<%# Bind("SiteID") %>'
             AppendDataBoundItems="True">
             <asp:ListItem Value=""></asp:ListItem>
         </asp:DropDownList>
         <asp:SqlDataSource ID="dsSubPlotNames" runat="server" 
             ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand="exec [339_PPM].usp_SubPlotNames_Select @100mPlotSiteID;">
             <SelectParameters>
                 <asp:Parameter Name="100mPlotSiteID" />
             </SelectParameters>
          </asp:SqlDataSource>
       </EditItemTemplate>
       <ItemTemplate>
           <asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot")%>'></asp:Label>
       </ItemTemplate>
</asp:TemplateField>

如您所见,参数名称"100mPlotSiteID"需要取自 100mPlot 列。我不知道如何清楚地描述这一点,如果您有任何问题,请告诉我。感谢您的帮助。

使用新修订的代码进行编辑。现在这么近!

<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 事件。

将参数传递给绑定到 gridview 更新中的下拉列表的 sqldatasource

您需要放置一个标签并将 text 属性设置为 '<%# Eval("100mPlot") %>',并在数据源中使用控件参数表。

如果标签的 id 是"label1"(必须在编辑项模板内(,则使用此

<asp:ControlParameter ControlID="label1" PropertyName="Text" Name="100mPlotSiteID" />