【发布时间】:2014-04-12 06:51:41
【问题描述】:
我的问题:
UpdatePanel 包含一个 DropDownList 和一个 GridView。 GridView 根据存储在 Label 中的值和从 DropDownList 中选择的值进行填充。它不会在 PageLoad 上填充;它仅在存在查询字符串或按下按钮时填充,因此!IsPostBack 中没有代码。 GridView 填充了 DropDownList 的初始 SelectedValue,我希望 GridView 在 DropDownList 的 SelectedValue 更改时重新填充。
问题是……它确实改变了!但只有一次。 GridView 最初使用 DropDownList 的默认 SelectedValue 的值填充数据。当 SelectedValue 发生变化时,GridView 中的数据也会发生变化。但只有一次。第二次或更多次更改 DropDownList 值后,没有任何反应。
<asp:UpdatePanel runat="server" ID="theUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:DropDownList runat="server" ID="theDropDownList" OnSelectedIndexChanged="theDropDownList_OnSelectedIndexChanged" EnableViewState="true" AutoPostBack="true" />
<asp:GridView ID="theGridView" runat="server" AutoGenerateColumns="False">
<Columns>
...
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
UpdatePanel 的 UpdateMode 是有条件的,因此它会在其子项 PostBack 时更新。 ChildrenAsTriggers 为 true,因此子项导致它更新。
theDropDownList 的 AutoPostBack 为 true,因此它会在更改时回发。 EnableViewState 为真(假使它甚至不加载)。 SelectedIndexChange 链接到正确的函数,我知道在调试模式下通过在其中放置一个中断来调用它。
这里是 SelectedIndexChanged 方法:
public void theDropDownList_OnSelectedIndexChanged(object sender, EventArgs e)
{
//get value that is stored in theLabel - I know this value is correct every time
int theValueFromTheLabel = Int32.Parse(theLabel.Text);
//populate theGridView with data from the DB
theGridView_Populate(theValueFromTheLabel);
//update theUpdatePanel (it works for the first change whether this line is here or not)
theUpdatePanel.Update();
}
我尝试了使用updatepanel.Update(); 方法和不使用updatepanel.Update(); 方法,结果是一样的:theDropDownList 的值的第一次更改有效,随后的每一次更改都不起作用。
实际填充 GridView 的函数非常简单:
protected void theGridView_Populate(int theValueFromTheLabel)
{
//get value from theDropDownList - this value does change when theDropDownList's value changes
int theValueFromTheDropDownList = Int32.Parse(theDropDownList.SelectedValue);
//get data from DB - the data here does change every time theDropDownList's value changed
ComplexClassController controller = new ComplexClassController();
List<ComplexClass> data = controller.GetData(theValueFromTheLabel, theValueFromTheDropDownList);
//load theGridView - this changes the data, but doesn't refresh theGridView to be able to see it
theGridView.DataSource = data;
theGridView.DataBind();
}
有什么想法吗?我一定是误解了 UpdatePanel 背后的事件。
【问题讨论】:
标签: c# asp.net gridview drop-down-menu updatepanel