问题
Though this seems to be discussed before, I can't apply given help in my case:
I have 2 DropDownLists (DDL1, DDL2) inside the Edit Template of a Gridview, which is bound to a SQLDataSource. DDL2 depends on the selected value of DDL1.
If I place the SQLDataSources that populate the DropDownLists on the top level, the ControlID of DDL2's ControlParameter isn't found even though I address it correctly (Gridview$DDL1ControlID). Therefore I placed the SQLDataSources inside the Edit Template of the Gridview. Now both DropDownLists are populated correctly if I enter in Edit Mode for a given Record; if I change the Index for DDL1, DDL2 is not updated automatically, as DDL2 has AutoPostBack set to false. Once I set AutoPostBack to true, the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the " is thrown.
<asp:GridView runat="server" ID="Gridview1"
DataSourceID="Milestones"
DataKeyNames="ID"
OnRowEditing="OnRowEditing"
OnRowDataBound="OnRowDataBoundMS"
OnSelectedIndexChanged="OnSelectedIndexChangedMS">
....
<asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="select function, 2 as ord
from Staff
where function is not null
group by function
union all
select 'select' as function, 0 as ord
union all
select '-----' as function, 1 as ord
order by ord, function">
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwnerGroup" Text='<%# Bind("DefaultOwnerGroup") %>' DataSourceID="OwnerGroups" DataTextField="function" DataValueField="function" Font-Size="12px" Width="80px" AutoPostBack="true"/>
</EditItemTemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="select Name, UserID
from Staff
where function = @OwnerGroup
union all
select Null as Name, Null as UserID
order by Name">
<SelectParameters>
<asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwner" Text='<%# Bind("DefaultOwner") %>' Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
</EditItemTemplate>
</asp:templatefield>
回答1:
I bind the DropdownLists in the OnRowDataBound Event like this:
<asp:templatefield HeaderText="Default Owner Group" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroup" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwnerGroup") %>' id="LabelDefaultOwnerGroupEdit" Visible="false"/>
<asp:SqlDataSource runat="server" id="OwnerGroups" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="...">
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwnerGroup" DataSourceID="OwnerGroups" DataTextField="funcion" DataValueField="funcion" Font-Size="12px" Width="80px" AutoPostBack="true"/>
</EditItemTemplate>
</asp:templatefield>
<asp:templatefield HeaderText="Default Owner" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwner" Font-Size="12px" Width="50px"/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Bind("DefaultOwner") %>' id="LabelDefaultOwnerEdit" Visible="false"/>
<asp:SqlDataSource runat="server" id="Owner" ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXXXX"
SelectCommand="...."
<SelectParameters>
<asp:ControlParameter ControlID="DDOwnerGroup" PropertyName="SelectedValue" Name="OwnerGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" id="DDOwner" Width="100px" DataSourceID="Owner" DataTextField="Name" DataValueField="UserID" Font-Size="12px"/>
</EditItemTemplate>
</asp:templatefield>
protected void OnRowDataBoundMS(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DDL1 = e.Row.FindControl("DDOwnerGroup") as DropDownList;
DropDownList DDL2 = e.Row.FindControl("DDOwner") as DropDownList;
Label LBL1 = e.Row.FindControl("LabelDefaultOwnerGroupEdit") as Label;
Label LBL2 = e.Row.FindControl("LabelDefaultOwnerEdit") as Label;
DDL1.SelectedValue = Convert.ToString(LBL1.Text);
DDL2.DataBind();
DDL2.SelectedValue = Convert.ToString(LBL2.Text);
}
}
}
来源:https://stackoverflow.com/questions/39206045/2-dropdownlists-in-gridview-cause-error-databinding-methods-such-as-bind-can