【发布时间】:2017-01-05 11:20:37
【问题描述】:
虽然这似乎之前讨论过,但我无法在我的情况下申请给予帮助:
我在绑定到 SQLDataSource 的 Gridview 的编辑模板中有 2 个 DropDownLists(DDL1、DDL2)。 DDL2 取决于 DDL1 的选定值。
如果我将填充 DropDownLists 的 SQLDataSources 放在顶层,即使我正确地处理了它(Gridview$DDL1ControlID),也找不到 DDL2 的 ControlParameter 的 ControlID。因此,我将 SQLDataSources 放置在 Gridview 的编辑模板中。现在,如果我进入给定记录的编辑模式,两个 DropDownLists 都会正确填充;如果我更改 DDL1 的索引,则 DDL2 不会自动更新,因为 DDL2 的 AutoPostBack 设置为 false。一旦我将 AutoPostBack 设置为 true,就会抛出错误“Eval()、XPath() 和 Bind() 等数据绑定方法只能在其中使用”。
<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>
【问题讨论】:
-
好的,-愚蠢-我明白了...我不必绑定DropDownList中的DataField;相反,我必须在后面的代码中设置 SelectedValue。放置一个 HiddenField 可以很容易地检索该值。
标签: asp.net gridview data-binding drop-down-menu sqldatasource