mctuna
mctuna

Reputation: 871

Selecting an asp.net control which is in an UpdatePanel

I want to enable a DropDownList which is in a ListView (ID = "SehensList") and put in an UpdatePanel. My first bet was the following, but it didn't work;

DropDownList DropdownDistrict = (DropDownList)SehenList.InsertItem.FindControl("DistrictDropDownListInsert");
        DropdownDistrict.Enabled = true;

Here is the aspx side;

 <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
            </td>
            <td>
                <asp:TextBox ID="CityFKTextBox_Insert" runat="server" Visible="false" Text='<%# Bind("CityFK") %>' />
                <asp:DropDownList ID="CityFKDropDownListInsert" runat="server" DataSourceID="CityFKEntityDataSource_Insert" AutoPostBack="true"
                            OnSelectedIndexChanged="CityFKDropDownListInsert_SelectedIndexChanged" DataTextField="CityName" DataValueField="CityID" 
                            AppendDataBoundItems="true">
                            <asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem>
                </asp:DropDownList>
                <asp:EntityDataSource ID="CityFKEntityDataSource_Insert" runat="server"
                            ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities"
                            EntitySetName="Cities">
                </asp:EntityDataSource> 
                <asp:ScriptManager ID="SMCityFKInsert" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UPCityFKInsert" runat="server">
                    <ContentTemplate>


                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="CityFKDropDownListInsert" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
            <td>
                <asp:TextBox ID="DistrictTextBox_Insert" runat="server" Visible="false" Text='<%# Bind("District") %>' />                    

                <asp:DropDownList ID="DistrictDropDownListInsert" runat="server" Enabled="false" AutoPostBack="true"
                      OnSelectedIndexChanged="DistrictDropDownListInsert_SelectedIndexChanged" DataTextField="DistrictName" DataValueField="DistrictID" 
                      AppendDataBoundItems="true">
                      <asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem>
                </asp:DropDownList>

                <asp:UpdatePanel ID="UPDistrictInsert" runat="server">
                    <ContentTemplate>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="DistrictDropDownListInsert" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>

            </td>
        </tr>
    </InsertItemTemplate>

As you notice DropDownLists are outside of the UpdatePanel and are called through "Triggers". If I put the DropDownLists into the UpdatePanel ContentTemplate (I guess, this is a totally wrong approach), second DropDownList "DistrictDropDownListInsert" is enabled but at that case it is not updated more than once. I mean by "not updated more than once" if you change the first DropDownList "CityFKDropDownListInsert" it is set to its previous value (not the default value but the first selected value). I know it's a bit confusing. If you have any unclear part please let me know.

Upvotes: 1

Views: 266

Answers (1)

afzalulh
afzalulh

Reputation: 7943

It should be like this:

protected void SehenList_ItemInserting(object sender, ListViewInsertEventArgs e)
{
    var pnl = SehenList.InsertItem.FindControl("UPCityFKInsert") as UpdatePanel;

    if (pnl != null)
    {
        var ddlDistrictInsert = pnl.FindControl("DistrictDropDownListInsert") as DropDownList;
        if (ddlDistrictInsert != null) ddlDistrictInsert.Enabled = true;
    }
}

Upvotes: 1

Related Questions