【问题标题】:ASP.NET C# - Do you need seperate datasources for each gridview of different tables in same database?ASP.NET C# - 您是否需要为同一数据库中不同表的每个 gridview 提供单独的数据源?
【发布时间】:2011-03-10 15:42:13
【问题描述】:

抱歉,这是一个菜鸟问题,但是如果每个 gridview 都访问同一个数据库但数据库中的表不同,您是否需要为每个 gridview 单独的数据源?

例如:我有 3 个不同的网格视图显示搜索 1) 给定 ZipCode 和 PlanCode,2) 给定 ZipCode、PlanCode 和 Age,3) 给定州代码和运营商代码。

这是 c# 代码隐藏:

protected void Search_Zip_Plan_Age_Button_Click(object sender, EventArgs e)
{
    var _with1 = this.ZipPlan_SqlDataSource;
    _with1.SelectParameters.Clear();

    string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["PriceFinderConnectionString"]
    _with1.SelectCommand = "ssp_get_zipcode_plan";
    _with1.SelectParameters.Add("ZipCode", this.ZipCode.Text);
    _with1.SelectParameters.Add("PlanCode", this.PlanCode.Text);
    _with1.SelectParameters.Add("Age", this.Age.Text);        

    _with1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;

    _with1.CancelSelectOnNullParameter = false;


    Search_Results_GridView.DataBind();
}

谢谢!

【问题讨论】:

  • 您也可以完全避免使用 DataSource 控件并进行手动绑定。我通常编写一个数据访问层,然后公开函数,这些函数返回绑定到控件所需的数据的各种视图。
  • @FrankHale,感谢您的回复。您是否使用具有该方法和 dll 文件的存储过程?如果您有时间,请发送所有文件所需的编码示例作为答案。您对我收到的 System.Configuration.ConfigurationManager.AppSettings 错误有任何想法吗?
  • 尝试将 () 放在对 .ToString 的调用之后。看看这是否能清除来自 ConfigurationManager 行的错误。
  • 就数据访问层而言,我为从我的 Web 应用程序引用的内容创建了一个新的类库。我将其功能保留在交互和操作数据库所需的功能上。如果您愿意,您可以使用存储过程,然后通过您使用的任何数据库访问方法与它们交互。我在这里使用 Linq to SQL。
  • @FrankHale,你能给我发个链接吗?什么是林克?我发现了问题。我将其重写为: string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["PriceFinderConnectionString"]

标签: c# asp.net gridview sqldatasource


【解决方案1】:

只有在查询相同的情况下,您才能重用数据源控件;否则,您需要不同的数据源控件。

HTH。

【讨论】:

  • 感谢您的快速回复! :)
猜你喜欢
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多