views:

1295

answers:

5

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 getting the selected value dropdownlist during postback:

private m_key;

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

    string xml_data;
    if (!IsPostBack)
    {
        xml_data = GetMyXml(0); // default value
        MyXmlDataSource.Data = xml_data;
        MyDdlDataSource.Data = xml_data;
    }
    else
    {
        GetSelections();
        xml_data = GetMyXml(m_key);
        MyXmlDataSource.Data = xml_data;
        MyXmlDataSource.DataBind();
    }
}

private void GetSelections()
{
    DropDownList l_MyDdl = FindMyControl<DropDownList>("MyDdl");
    if (l_MyDdl != null)
        if (!Int32.TryParse(l_MyDdl.SelectedItem.Value, out m_key))
            m_key = 0;
}

Everything works great, up until a postback as a result of the dropdown list changing occurs. When this happens, I get the value of the selected item in the dropdown list, pass it to my GetMyXml() method with the value from the dropdown list as a parameter and then set the FormView's datasource to the newly returned XML data from GetMyXml(). I've looked at the value of "xml_data" during postback and it's definitely correct. However, the values displayed on the page the FormView (like XPath("SomeNode")) are the values from before the postback happened and not the ones returned in xml_data. Why would this happen and how would I go about resolving it? Thanks in advance.

A: 

even i am also having the same issue? I made this property in xmldatasource EnableCaching="false" ... eventhough it is not binding the latest data got from XML.

A: 

You could try MyFormView.DataBind() after MyXmlDataSource.DataBind();

sirrocco
For some reason, I get a null reference exception when I try that. Why would that be?
Bullines
I'm not really sure, you should update the question to reflect that. Someone might have an answer.
sirrocco
A: 

in order to automatically rebind you must have the ViewState enabled on that control.

craigmoliver
A: 

Hey all try this

formView.Databind(); this.page.databind();

Kishh
A: 

You don't have to do any manual data binding in the coe code behind when you're using a DataSource object. Implement the data source's OnSelecting event and call GetSelections within that method.

Edit: I was too fast here. XmlDataSource does not an OnSelecting event. It has an OnDataBind, but here the event argument is just a standard EventArg, so I don't know how you would bind the result from GetSelections to it. Sorry

Jan Aagaard