2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control
2 Dropdownlists in Gridview cause error: Databinding methods such as Bind() can only be used in the context of a databound control
虽然之前似乎讨论过这个问题,但我无法在我的案例中申请 given help:
我在 Gridview 的编辑模板中有 2 个下拉列表(DDL1、DDL2),它们绑定到 SQLDataSource。 DDL2 取决于 DDL1 的选定值。
如果我将填充 DropDownLists 的 SQLDataSources 放在顶层,即使我正确地寻址它 (Gridview$DDL1ControlID),也找不到 DDL2 的 ControlParameter 的 ControlID。因此,我将 SQLDataSources 放在 Gridview 的编辑模板中。现在,如果我进入给定记录的编辑模式,两个 DropDownLists 都会正确填充;如果我更改 DDL1 的索引,DDL2 不会自动更新,因为 DDL2 已将 AutoPostBack 设置为 false。一旦我将 AutoPostBack 设置为 true,就会抛出错误 "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the "。
<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>
我像这样在 OnRowDataBound 事件中绑定 DropdownLists:
<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);
}
}
}
虽然之前似乎讨论过这个问题,但我无法在我的案例中申请 given help:
我在 Gridview 的编辑模板中有 2 个下拉列表(DDL1、DDL2),它们绑定到 SQLDataSource。 DDL2 取决于 DDL1 的选定值。
如果我将填充 DropDownLists 的 SQLDataSources 放在顶层,即使我正确地寻址它 (Gridview$DDL1ControlID),也找不到 DDL2 的 ControlParameter 的 ControlID。因此,我将 SQLDataSources 放在 Gridview 的编辑模板中。现在,如果我进入给定记录的编辑模式,两个 DropDownLists 都会正确填充;如果我更改 DDL1 的索引,DDL2 不会自动更新,因为 DDL2 已将 AutoPostBack 设置为 false。一旦我将 AutoPostBack 设置为 true,就会抛出错误 "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the "。
<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>
我像这样在 OnRowDataBound 事件中绑定 DropdownLists:
<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);
}
}
}