更新面板中的Gridview未反映使用ObjectDataSource的更改

时间:2015-04-19 06:49:39

标签: c# asp.net gridview objectdatasource

我有一个asp.net GridView,它使用ObjectDataSource来获取数据。 GridView位于UpdatePanel内部。我想点击不同的LinkBut​​tons过滤数据。为此,我为每个LinkBut​​ton编写了新的SelectMethodSelectCountMethod,并在点击LinkBut​​ton时将这些方法设置为ObjectDataSource。设置两个方法后,我调用GridView.DataBind()方法。所有SelectMethod返回类型均为DataSet

我调试了代码,我从数据库中获取数据,但gridview没有更新。单击LinkBut​​tons时,GridView_DataBound事件也不会发生。以下我试过的东西不起作用:

  • 设置LinkBut​​ton CausesValidation="True"
  • 设置UpdatePanel UpdateMode="Always"
  • 使用SelectMethodSelectCountMethod创建了ObjectDataSources 对于每个LinkBut​​ton并将GridView.DataSource设置为单击 链接按钮。
  • 还尝试点击链接按钮设置FilterExpressionFilterParameters。这确实会发生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>

LinkBut​​ton点击

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();
        }

1 个答案:

答案 0 :(得分:1)

遇到了问题。发布解决方案以防有人需要它。

问题在于ObjectDataSource的SelectMethod。当我单击LinkBut​​ton时,Update面板重置SelectMethod并使用在设计时定义的。

我通过对所有LinkBut​​ton仅使用1个SelectMethod来解决它,并使用会话参数传递给SelectMethod并按会话值获取数据。