Cannot reformat FormView control on postback
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 to get the dropdown of the selected value 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 fine until postback occurs as a result of changing the dropdown list. When this happens, I get the value of the selected item in the dropdown, pass it to the GetMyXml () method with the value from the dropdown as a parameter, and then set the FormView datasource for the newly returned XML data from GetMyXml (). I looked at the value of "xml_data" during postback and it is definitely correct. However, the values โโdisplayed on the FormView (for example, XPath ("SomeNode")) are the values โโbefore the postback was performed, not the ones returned in xml_data. Why is this happening and how can I solve it? Thanks in advance.
source to share
You don't need to do data binding manually in factor code when you use the DataSource object. Inject data source OnSelecting event and call GetSelections
inside this method.
Edit: I was too quick here. The XmlDataSource is not an OnSelecting event. It has OnDataBind, but the event argument is standard here EventArg
, so I don't know how you bind the result from GetSelections to it. Unfortunately
source to share