My gridview contains a column called SubPlot. In Edit mode, this column is supposed to show a dropdownlist that binds to a datasource. This datasource takes a parameter called @100mPlotName. This is the error I get when I click on "Edit"
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
ASP.NET:
<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 name="100mPlotName" />
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Code behind:
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);
}
}
I feel like I'm missing something in the code behind. My goal is to show the dropdownlist with data from the datasource when the user clicks on edit. Thanks for your help.