首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用按钮单击更改LinqDataSource

使用按钮单击更改LinqDataSource
EN

Stack Overflow用户
提问于 2012-10-31 15:41:07
回答 2查看 928关注 0票数 0

我的asp页面上有一个GridView。我想通过单击按钮来更改LinqDataSource。这是因为我有2个数据库视图,你必须能够看到其中一个视图,如你所愿。我的问题是,当我试图将GridView绑定到我的任何LinqDataSource时,什么都没有发生。

我的C#代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<%@ 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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-31 16:01:37

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

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
  if(!isPostBack) {
       this.Grid.DataSource = lqds_Grid1;
       this.Grid.DataBind();
   }
}

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

票数 1
EN

Stack Overflow用户

发布于 2012-10-31 15:58:32

首先,您可以尝试将数据源设置为null,然后再为其赋予新值,并且在分配新值之后,您可以调用datagrid的刷新方法来强制它重新绘制自身

代码语言:javascript
复制
this.Grid.DataSource = null;
this.Grid.DataSource = lqds_Grid1;
this.Grid.DataBind();
this.Grid.DataSource.Refresh();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13153322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档