views:

1933

answers:

1

Greetings!

I have a DropDownList within a FormView which are 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 page's codebehind, I have the following OnLoad() method as well as the method for when the select index of the dropdownlist 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;
}

When a different value is selected from the dropdown list and SelectedIndexChanged is invoked, I am unable to get the value of the dropdown list (FindControl always returns null) in order to use it to re-bind the datasources. How can I get this value?

+1  A: 

Because your dropdownlist is contained within another control it may be that you need a recursive findcontrol.

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

TT
That's pretty slick. Thanks :)
Bullines