简体   繁体   中英

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.

为了自动重新绑定,您必须在该控件上启用ViewState。

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

您可以在MyFormView.DataBind()之后尝试MyXmlDataSource.DataBind();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM