0

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.

4

4 回答 4

1

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

于 2008-12-29T16:00:41.983 回答
0
formView.Databind();
this.page.databind();
于 2009-01-03T06:43:50.953 回答
0

MyFormView.DataBind()之后你可以试试MyXmlDataSource.DataBind();

于 2008-12-11T06:38:11.397 回答
0

当您使用 DataSource 对象时,您不必在后面的 coe 代码中进行任何手动数据绑定。实现数据源的 OnSelecting 事件并GetSelections在该方法中调用。

编辑:我在这里太快了。XmlDataSource 没有 OnSelecting 事件。它有一个 OnDataBind,但这里的事件参数只是一个标准EventArg,所以我不知道如何将 GetSelections 的结果绑定到它。对不起

于 2009-12-04T21:07:09.300 回答