GridView未从UpdatePanel内DropDownList的SelectedIndexChange事件进行更新
本文关键字:事件 更新 SelectedIndexChange 未从 UpdatePanel DropDownList GridView | 更新日期: 2023-09-27 17:57:56
我的问题:
UpdatePanel包含一个DropDownList和一个GridView。GridView基于存储在Label中的值和从DropDownList中选择的值进行填充。它不会在PageLoad上填充;它只在存在查询字符串或按下按钮时填充,因此!IsPostBack
中没有代码。GridView是用DropDownList的初始SelectedValue填充的,我希望在DropDownList的SelectedValue更改时重新填充GridView。
问题是…它确实改变了!但只有一次。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是Conditional,因此它在其子级PostBack时进行更新。ChildrenAsTriggers是真的,所以孩子会导致它更新。
DropDownList的AutoPostBack为true,因此它将在更改时回发。EnableViewState为true(false使其甚至不加载)。SelectedIndexChange链接到正确的函数,我知道它是通过在调试模式中插入一个break来调用的。
这是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();
方法,结果都是一样的:DropDownList值的第一次更改有效,接下来的每一次更改都无效。
实际填充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背后的事件。
为什么不直接使用((DropDownList)sender).selectedValue获取您的值来编辑网格视图?
Kev
从下拉列表中获取值似乎没有任何问题,原因可能是您的组合的给定数据源
放入大量组合!页面加载事件的回发类似于
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback){
Loadcomobo();
}
}
如果不是这样的话,那么你可以把页面加载事件代码放在这里吗谢谢
我仍然不确定为什么它更新了一次,然后再也不会更新了,但通过将DropDownList和GridView放在它们自己的UpdatePanel中而不是放在同一个UpdatePanel中,问题已经得到了解决。
尝试向更新面板添加触发器
<triggers>
<asyncpostback ControlID="theDropDownList" />
</triggers>
或者类似之前的东西
</asp:UpdatePanel>