我有一個asp.net GridView,它使用ObjectDataSource來獲取數據。 GridView在UpdatePanel中。我想要點擊不同的LinkButtons來過濾數據。爲此,我爲每個LinkButton編寫了新的SelectMethod
和SelectCountMethod
,並通過單擊LinkButton將這些方法設置爲ObjectDataSource。設置好這兩種方法後,我打電話給GridView.DataBind()
方法。所有SelectMethod
返回類型是DataSet
。更新面板中的Gridview不反映使用ObjectDataSource的變化
我調試了代碼,我從數據庫中獲取數據,但gridview沒有更新。當單擊LinkButton時,GridView_DataBound
事件也不會發生。以下的事情我嘗試過哪些不起作用:
- 設置LinkButton
CausesValidation="True"
。 - 設置UpdatePanel
UpdateMode="Always"
。 - 創建ObjectDataSources爲
SelectMethod
和SelectCountMethod
爲每個LinkButton和設置GridView.DataSource
點擊 鏈接按鈕。 - 還嘗試設置
FilterExpression
和FilterParameters
點擊鏈接按鈕。這確實會擊中ObjectDataSource1_Filtering
事件並且正確的參數正在傳遞。
GridView控件的UpdatePanel與
<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();
}