简体   繁体   English

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

[英]Passing parameter to sqldatasource that's bound to a dropdownlist in a gridview update

I have an editable gridview with many columns. 我有许多列的可编辑gridview。 Two of which are 100mPlot and SubPlot. 其中两个是100mPlot和SubPlot。 SubPlot's are not unique (1A, 1B, 1C, and 1D) but 100mPlot + SubPlot make a unique set of values. 子图不是唯一的(1A,1B,1C和1D),但是100mPlot + SubPlot构成一组唯一的值。

When a user clicks "Edit", they will see a dropdown list in the SubPlot column. 当用户单击“编辑”时,他们将在“子图”列中看到一个下拉列表。 This list needs to be based on what the value of 100mPlot is. 该列表需要基于100mPlot的值。 So in order to display the correct values for SubPlot, I need to pass in a value from 100mPlot column as a parameter to the sqldatasource that would be bound to the SubPlot dropdownlist. 因此,为了显示SubPlot的正确值,我需要将100mPlot列中的值作为参数传递给将绑定到SubPlot下拉列表的sqldatasource。 How would I go about this? 我将如何处理?

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

As you can see, the parameter name "100mPlotSiteID" needs to be taken from 100mPlot column. 如您所见,参数名称“ 100mPlotSiteID”需要从100mPlot列中获取。 I'm not sure how else to describe this anymore clearly, let me know if you have any questions. 我不确定该如何进一步清楚地描述这一点,如果您有任何疑问,请告诉我。 Thanks for your help. 谢谢你的帮助。

EDIT with newly revised code. 使用新修改的代码进行编辑。 So close now! 现在关闭!

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

Unfortunately, I get this error with the Eval("100mPlot"): 不幸的是,我收到Eval(“ 100mPlot”)的错误消息:

Databinding expressions are only supported on objects that have a DataBinding event. 只有具有DataBinding事件的对象才支持数据绑定表达式。 System.Web.UI.WebControls.ControlParameter does not have a DataBinding event. System.Web.UI.WebControls.ControlParameter没有DataBinding事件。

You need to put a label and set the text property to '<%# Eval("100mPlot") %>' and use a control paremeter in dthe datasource. 您需要放置标签,并将文本属性设置为'<%# Eval("100mPlot") %>'然后在数据源中使用控件参数。

If the label's id is "label1" (must be inside edit item template) then use this 如果标签的ID为“ label1”(必须在编辑项模板的内部),请使用此标签

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM