Can't Rebind FormView Control on Postback

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.

11.12.2008 01:31:55
4 ОТВЕТА

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

0
12.04.2013 06:34:19
For some reason, I get a null reference exception when I try that. Why would that be?
Bullines 11.12.2008 14:14:09
I'm not really sure, you should update the question to reflect that. Someone might have an answer.
sirrocco 12.12.2008 05:09:02

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

1
29.12.2008 16:00:41
formView.Databind();
this.page.databind();
0
12.04.2013 06:34:06

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

0
4.12.2009 21:17:40