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.