dam
dam

Reputation: 171

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

I have an editable gridview with many columns. Two of which are 100mPlot and SubPlot. SubPlot's are not unique (1A, 1B, 1C, and 1D) but 100mPlot + SubPlot make a unique set of values.

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. 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. 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. 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"):

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ControlParameter does not have a DataBinding event.

Upvotes: 1

Views: 6003

Answers (1)

Krishanu Dey
Krishanu Dey

Reputation: 6406

You need to put a label and set the text property to '<%# Eval("100mPlot") %>' and use a control paremeter in dthe datasource.

If the label's id is "label1" (must be inside edit item template) then use this

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

Upvotes: 5

Related Questions