0

ASPページにGridViewがあります。ボタンのクリックで LinqDataSource を変更したい。これは、2 つのデータベース ビューがあり、これらのビューの 1 つを必要に応じて表示できる必要があるためです。私の問題は、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 に設定し、新しい値を割り当てた後、データグリッドの refresh メソッドを呼び出して強制的に再描画することができます

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