我有一个asp.net GridView,它使用ObjectDataSource来获取数据。 GridView位于UpdatePanel内部。我想点击不同的LinkButtons过滤数据。为此,我为每个LinkButton编写了新的SelectMethod
和SelectCountMethod
,并在点击LinkButton时将这些方法设置为ObjectDataSource。设置两个方法后,我调用GridView.DataBind()
方法。所有SelectMethod
返回类型均为DataSet
。
我调试了代码,我从数据库中获取数据,但gridview没有更新。单击LinkButtons时,GridView_DataBound
事件也不会发生。以下我试过的东西不起作用:
CausesValidation="True"
。UpdateMode="Always"
。 SelectMethod
和SelectCountMethod
创建了ObjectDataSources
对于每个LinkButton并将GridView.DataSource
设置为单击
链接按钮。FilterExpression
和FilterParameters
。这确实会发生ObjectDataSource1_Filtering
事件并且正确的参数正在传递。使用UpdatePanel的GridView
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<asp:GridView ID="GridView" runat="server"
AllowPaging="True" AllowSorting="True" CssClass="grdViewHeaderDivider"
AutoGenerateColumns="True"
OnSorting="GridView_Sorting"
OnDataBound="GridView_DataBound"
OnPageIndexChanging="GridView_PageIndexChanging"
GridLines="None"
DataSourceID="ObjectDataSource1">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView" EventName="Sorting" />
<asp:AsyncPostBackTrigger ControlID="GridView" EventName="PageIndexChanging" />
<asp:AsyncPostBackTrigger ControlID="GridView" EventName="DataBound" />
<asp:AsyncPostBackTrigger ControlID="lblTotalUploaded" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lblUploadedNS" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lblUploadedSA" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
的ObjectDataSource
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OnFiltering="ObjectDataSource1_Filtering" SelectCountMethod="GetApplicationsCount" SelectMethod="GetApplications" TypeName="TestApp.Applications" SortParameterName="sortExpression" EnableCaching="false" EnablePaging="True" >
<SelectParameters>
</SelectParameters>
</asp:ObjectDataSource>
LinkButton点击
protected void lblTotalUploaded_Click(object sender, EventArgs e)
{
var btn = (LinkButton)sender;
//ObjectDataSource1.FilterExpression = "ApplicationName='{0}'";
switch (btn.CommandName)
{
case "lblTotalUploaded":
ObjectDataSource1.SelectMethod = "GetApplications";
ObjectDataSource1.SelectCountMethod = "GetApplicationsCount";
break;
case "lblUploadedSA":
ObjectDataSource1.SelectMethod = "GetScore";
ObjectDataSource1.SelectCountMethod = "GetScoreCount";
//ObjectDataSource1.FilterParameters.Add("ApplicationName", "Candy Crush");
break;
case "lblUploadedNS":
ObjectDataSource1.SelectMethod = "GetNoScore";
ObjectDataSource1.SelectCountMethod = "GetNoScoreCount";
//ObjectDataSource1.FilterParameters.Add("ApplicationName", "Clash of Clans");
break;
}
ObjectDataSource1.Select();
GridView.DataBind();
}
答案 0 :(得分:1)
遇到了问题。发布解决方案以防有人需要它。
问题在于ObjectDataSource的SelectMethod。当我单击LinkButton时,Update面板重置SelectMethod并使用在设计时定义的。
我通过对所有LinkButton仅使用1个SelectMethod来解决它,并使用会话参数传递给SelectMethod并按会话值获取数据。