Rebind to postback based on selected DropDown value

Hello!

I have a DropDownList on a FormView, bound to XmlDataSources:

<asp:FormView ID="MyFormView" runat="server" DataSourceID="MyXmlDataSource">
    <ItemTemplate>
        <h1><%# XPath("SomeNode")%></h1>
        <asp:Label ID="MyLabel" runat="server" AssociatedControlID="MyDdl" Text='<%# XPath("SomeOtherNode")%>' />
        <asp:DropDownList ID="MyDdl"
                          runat="server"
                          DataSourceID="MyDdlDataSource"
                          DataTextField="name"
                          DataValueField="value"
                          AutoPostBack="true"
                          OnSelectedIndexChanged="MyDdl_SelectedIndexChanged">
        </asp:DropDownList>
    </ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" XPath="Root/MainSection" />
<asp:XmlDataSource ID="MyDdlDataSource" runat="server" XPath="Root/MainSection/Areas/*" />

      

In the codebehind of the page, I have the following OnLoad () method as well as a method when the dropdown selection index changes:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    if (!IsPostBack)
    {
        string xml = GetMyXml(0); // default value
        MyXmlDataSource.Data = xml;
        MyDdlDataSource.Data = xml;
    }
}

protected void MyDdl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList l_MyDdl = FindControl("MyDdl") as DropDownList;
    int myVal;
    if (l_MyDdl != null)
        if (!Int32.TryParse(l_MyDdl.SelectedItem.Value, out myVal))
            myVal = 0;
    string xml = GetMyXml(myVal);
    MyXmlDataSource.Data = xml;
    MyDdlDataSource.Data = xml;
}

      

If a different value is selected in the dropdown and SelectedIndexChanged is called, I cannot get the dropdown value (FindControl always returns null) to use to re-bind data sources. How can I get this value?

0


source to share


1 answer


Since your dropdown is contained in another control, you may need to recursively findcontrol.



http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx

+1


source







All Articles