无法在回发时重新绑定 FormView 控件
问候!
我在 FormView 中有一个 DropDownList,它绑定到 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/*" />
在页面的代码隐藏中,我有以下 OnLoad() 方法以及在回发期间获取所选值下拉列表的方法:
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;
}
一切都很好,直到回发为下拉列表的结果发生变化。 发生这种情况时,我获取下拉列表中所选项目的值,将其传递给 GetMyXml() 方法,并将下拉列表中的值作为参数,然后将 FormView 的数据源设置为从 GetMyXml( )。 我在回发期间查看了“xml_data”的值,它绝对是正确的。 但是,FormView 页面上显示的值(如 XPath("SomeNode"))是回发发生之前的值,而不是 xml_data 中返回的值。 为什么会发生这种情况以及我将如何解决它? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了自动重新绑定,您必须在该控件上启用 ViewState。
in order to automatically rebind you must have the ViewState enabled on that control.
当您使用 DataSource 对象时,无需在后面的 coe 代码中执行任何手动数据绑定。 实现数据源的 OnSelecting 事件并在该方法中调用
GetSelections
。编辑:我在这里太快了。 XmlDataSource 没有 OnSelecting 事件。 它有一个 OnDataBind,但这里的事件参数只是一个标准的 EventArg,所以我不知道如何将 GetSelections 的结果绑定到它。 对不起
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您可以在
MyXmlDataSource.DataBind();
之后尝试MyFormView.DataBind()
You could try
MyFormView.DataBind()
afterMyXmlDataSource.DataBind();