2

I have a DetailsView like this:

<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>

it has a field named Cliente hooked up to an EntityDataSource:

<asp:EntityDataSource ID="EntityDataSourceClienti" runat="server" ConnectionString="name=SalesPortalEntities"
    DefaultContainerName="SalesPortalEntities" EnableFlattening="False" EntitySetName="Clienti"
    OrderBy="it.NomeSocieta" EntityTypeFilter="Clienti">
</asp:EntityDataSource>

and I'd like to change another field, Contatto, when I change the selection of the Cliente field. The EntityDataSource for Contatto is:

<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>

Also, for completeness this is the EntityDataSource for the 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>

the code-behind is:

protected void ddlClienti_SelectedIndexChanged(object sender, EventArgs e)
{
    edsContatti.WhereParameters[0].DefaultValue = ((DropDownList)sender).SelectedValue;        
}

How do I hook ddlContatti up to ddlClienti in DetailsView1's insert and edit modes?

Below is a screenshot of the details view:

enter image description here

Thanks in advance.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
AlessandroG
  • 318
  • 5
  • 14

1 Answers1

1

One more try here, and then I will quit wasting your time if this doesn't work =)

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();
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • @AlessandroG Oops, I forgot about ControlParameters. This actually provides the functionality you're looking for with very little work. Let me know if that helps! – Josh Darnell Nov 08 '11 at 16:19
  • this was my first code, but if I start the page this error will appear: Could not find control 'ddlClienti' in ControlParameter 'IDClienteFromDdl'. – AlessandroG Nov 08 '11 at 16:32
  • @AlessandroG Bah, I forgot that ddl was inside the GridView. I'll update the answer if I think of something else. – Josh Darnell Nov 08 '11 at 16:42
  • @AlessandroG I gave this one more shot (since the solution is bugging me now haha). – Josh Darnell Nov 08 '11 at 17:04
  • @AlessandroG Ha! That actually works =) I just tested a small example on my side anyway. Let me know if you have any trouble with it. – Josh Darnell Nov 08 '11 at 17:45
  • 1
    Yes, it works changing ddlContatti.DataBind() with detailsView1.DataBind(); probably because the databinding is propagated towards the Children. Thank You J. – AlessandroG Nov 10 '11 at 10:31