0

我的 asp 页面上有一个 GridView。我想通过单击按钮来更改 LinqDataSource。这是因为我有 2 个数据库视图,您必须能够根据需要查看其中一个视图。当我尝试将 GridView 绑定到我的任何 LinqDataSource' 时,我的问题是没有任何反应。

我的 C# 代码:

protected void Page_Load(object sender, EventArgs e)
{
    this.Grid.DataSource = lqds_Grid1;
    this.Grid.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (this.Grid.DataSource == lqds_Grid1)
    {
        this.Grid.DataSource = lqds_Grid2;
        this.Grid.DataBind();
    }
    else
    {
        this.Grid.DataSource = lqds_Grid1;
        this.Grid.DataBind();
    }
}

我的 asp 代码:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AddressReporting._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:LinqDataSource ID="lqds_Grid1" runat="server" 
        ContextTypeName="AddressReporting.MobileGateway" EntityTypeName="" 
        OrderBy="AdrID, Country" TableName="BarcodeWithLocation">
    </asp:LinqDataSource>
    <asp:LinqDataSource ID="lqds_Grid2" runat="server" 
        ContextTypeName="AddressReporting.MobileGateway" EntityTypeName="" 
        OrderBy="AdrID, Country" TableName="BarcodeWithLocationSorted">
    </asp:LinqDataSource>
    <asp:GridView ID="Grid" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" Height="217px" Width="268px">
</asp:GridView>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</asp:Content>
4

2 回答 2

1

这是因为page_load方法(事件)工作时间页面加载所以不太正确所以

protected void Page_Load(object sender, EventArgs e)
{
  if(!isPostBack) {
       this.Grid.DataSource = lqds_Grid1;
       this.Grid.DataBind();
   }
}

你应该检查页面是否有回发

于 2012-10-31T08:01:37.280 回答
0

首先你可以尝试在给它新值之前将你的数据源设置为null,在分配新值之后你可以调用数据网格的刷新方法来强制它重绘自己

this.Grid.DataSource = null;
this.Grid.DataSource = lqds_Grid1;
this.Grid.DataBind();
this.Grid.DataSource.Refresh();
于 2012-10-31T07:58:31.817 回答