我有这样的DetailsView
:
<asp:DetailsView ID="detailsView1" runat="server"
DataSourceID="edsdetailsView" AutoGenerateRows="False" CssClass="pretty-table">
<Fields>
<asp:TemplateField HeaderText="Cliente">
<ItemTemplate>
<asp:Label ID="dw1Label2" runat="server" Text='<%# Bind("Clienti.NomeSocieta") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlClienti" runat="server" SelectedValue='<%# Bind("IDCliente") %>'
Width="300" DataSourceID="EntityDataSourceClienti" DataTextField="NomeSocieta"
AutoPostBack="true" DataValueField='IDCliente'
onselectedindexchanged="ddlClienti_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddlClienti" runat="server" SelectedValue='<%# Bind("IDCliente") %>'
Width="300" DataSourceID="EntityDataSourceClienti" DataTextField="NomeSocieta"
AutoPostBack="true" DataValueField='IDCliente'
onselectedindexchanged="ddlClienti_SelectedIndexChanged">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contatto" SortExpression="IDContatto">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Contatti.Nome") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlContatti" runat="server" SelectedValue='<%# Bind("IDContatto") %>'
Width="300px" DataSourceID="edsContatti" DataTextField="Nome"
DataValueField='IDContatto'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddlContatti" runat="server" SelectedValue='<%# Bind("IDContatto") %>'
Width="300" DataSourceID="edsContatti" DataTextField="Nome"
DataValueField='IDContatto'/>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
它具有一个连接到
Cliente
的名为EntityDataSource
的字段:<asp:EntityDataSource ID="EntityDataSourceClienti" runat="server" ConnectionString="name=SalesPortalEntities"
DefaultContainerName="SalesPortalEntities" EnableFlattening="False" EntitySetName="Clienti"
OrderBy="it.NomeSocieta" EntityTypeFilter="Clienti">
</asp:EntityDataSource>
当我更改
Contatto
字段的选择时,我想更改另一个字段Cliente
。 EntityDataSource
的Contatto
为:<asp:EntityDataSource ID="edsContatti" runat="server" ConnectionString="name=SalesPortalEntities"
DefaultContainerName="SalesPortalEntities" EnableFlattening="False" EntitySetName="Contatti"
Where="it.IDAzienda=@IDClienteFromDdl">
<WhereParameters>
<asp:Parameter Name="IDClienteFromDdl" Type="Int32"/>
</WhereParameters>
</asp:EntityDataSource>
另外,出于完整性考虑,这是
EntityDataSource
的DetailsView1
:<asp:EntityDataSource ID="edsdetailsView" runat="server" Include="Categorie,Commerciali,Clienti,Contatti"
ConnectionString="name=SalesPortalEntities" OnInserting="UpdateDataSource" DefaultContainerName="SalesPortalEntities"
EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
EntitySetName="PianificazioneIncontri" Where="it.IDPianificazione = @IDPianificazione AND ((it.Obsoleto=false AND it.ApprovazioneModifica=false AND it.ApprovazioneEliminazione=false) OR (@GruppoAdmin='1' AND it.Obsoleto=false))"
OnUpdating="edsdetailsView_Updating">
<WhereParameters>
<asp:ControlParameter ControlID="GridView1" Name="IDPianificazione" PropertyName="SelectedValue" Type="Int32" />
<asp:Parameter Name="GruppoAdmin" Type="String" />
</WhereParameters>
</asp:EntityDataSource>
后面的代码是:
protected void ddlClienti_SelectedIndexChanged(object sender, EventArgs e)
{
edsContatti.WhereParameters[0].DefaultValue = ((DropDownList)sender).SelectedValue;
}
如何在
ddlContatti
的插入和编辑模式下将ddlClienti
挂接到DetailsView1
?以下是详细信息视图的屏幕截图:
提前致谢。
最佳答案
再尝试一次,如果这样不起作用,我将放弃您的时间=)
protected void ddlClienti_SelectedIndexChanged(object sender, EventArgs e)
{
edsContatti.WhereParameters[0].DefaultValue = ((DropDownList)sender).SelectedValue;
// Call DataBind to reload the DataSource based on the new WHERE clause
edsContatti.DataBind();
// Since you're using a DetailsView, you have to find the nested control within it
DropDownList ddlContatti = (DropDownList)DetailsView1.FindControl("ddlConcatti");
// And then call DataBind on it as well
detailsView1.DataBind();
}