Gridview错误:定义了DataSource和DataSourceID-但我同时需要这两者

问题描述 投票:0回答:1

干杯,

我有一个ASP Web表单,可加载Gridview,“ Gridview1”在某些情况下向用户显示,而在其他情况下则隐藏。 Gridview是在设计器中创建的,并且Gridview.DataSourceID绑定到aspx文件中的SqlDataSource。

现在,我试图添加一个使用两个文本框和一个按钮单击事件的搜索功能,以从表中查询数据,并使用CodeBehind将Gridview1重新加载为较小的数据集。

SO和其他论坛上有很多关于此特定错误的文章,但我认为我既需要在CodeBehind中定义的DataSource,也需要定义要绑定在前端aspx文件中初始SqlDataSource的DataSourceID的DataSourceID。

是否可以同时使用两个定义?我试图用[

诱使CodeBehind放弃DataSourceID
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
GridView1.DataSourceID = null;
GridView1.DataSource = rdr;
GridView1.DataBind();

但是那只是引发了另一个错误。有没有办法定义两者?还是可以使用GridView1.DataSourceID而不是GridView1.DataSource将SQL结果绑定回Gridview?

这里是ASP页面:

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeFile="Junk2.aspx.cs" Inherits="CPITraining.Junk2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <style>
        .container {
            max-width: 1530px;
        }
        .niceLabels {
            margin:15px 0px 15px 0px;
            display:inline-block;
            font:normal 30px Arial;
        }
        </style>
<%-- TOP CONTAINER --%>
    <div class="container" style="width:1500px">
        <div class="row">
            <div class="col-md-12">
                <div style="background-color:#FFFFFF">
                    <%-- EMPLOYEE GridView1 Window --%>
                        <asp:Label ID="EmplList" runat="server" CssClass="niceLabels">Employee Listing</asp:Label>
                    <div>
                        <asp:TextBox ID="txtBadge" runat="server" OnInit="txtBadge_Init"></asp:TextBox>
                        <asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
                        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
                    </div>
                        <asp:GridView ID="GridView1" DataKeyNames="badge_id" runat="server" BackColor="White" BorderColor="#999999" 
                        BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" 
                        GridLines="Vertical" DataSourceID="SqlDataSource1" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                            <AlternatingRowStyle BackColor="#CCCCCC" />
                            <Columns>
                                <asp:CommandField ShowSelectButton="True" />
                                <asp:BoundField DataField="BADGE_ID" HeaderText="BADGE_ID" SortExpression="BADGE_ID" />
                                <asp:BoundField DataField="FIRSTNAME" HeaderText="FIRSTNAME" SortExpression="FIRSTNAME" />
                                <asp:BoundField DataField="LASTNAME" HeaderText="LASTNAME" SortExpression="LASTNAME" />
                                <asp:BoundField DataField="TITLE" HeaderText="TITLE" SortExpression="TITLE" />
                                <asp:BoundField DataField="deptname" HeaderText="deptname" SortExpression="deptname" />
                                <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" SortExpression="Column1" />
                            </Columns>
                            <FooterStyle BackColor="#CCCCCC" />
                            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                            <SortedAscendingHeaderStyle BackColor="#808080" />
                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                            <SortedDescendingHeaderStyle BackColor="#383838" />
                        </asp:GridView>
                </div>
            </div>
        </div>
    </div>
<%-- BOTTOM CONTAINER --%>
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <div style="background-color:#FFFFFF">
                        <%-- LEFT DETAILS VIEW --%>
                        <asp:Label ID="EmplHist" runat="server" CssClass="niceLabels">Employee History</asp:Label>
                        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="badge_id" 
                         BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                         DataSourceID="SqlDataSource2" ForeColor="Black" GridLines="Horizontal" Height="50px" Width="125px">
                            <EditRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                            <Fields>
                                <asp:BoundField DataField="BADGE_ID" HeaderText="BADGE_ID" SortExpression="BADGE_ID" />
                                <asp:BoundField DataField="FIRSTNAME" HeaderText="FIRSTNAME" SortExpression="FIRSTNAME" />
                                <asp:BoundField DataField="LASTNAME" HeaderText="LASTNAME" SortExpression="LASTNAME" />  
                                <asp:BoundField DataField="TITLE" HeaderText="TITLE" SortExpression="TITLE" />
                                <asp:BoundField DataField="deptname" HeaderText="deptname" SortExpression="deptname" />
                                <asp:BoundField DataField="Expr1" HeaderText="Expr1" ReadOnly="True" SortExpression="Expr1" />
                            </Fields>
                            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />  
                        </asp:DetailsView>
                </div>
            </div>
            <div class="col-md-9">
                <div class="row" style="background-color:#FFFFFF">
                     <%-- PROCEDURE CLASSES VIEW --%>
                    <asp:Label ID="ProcTraining" runat="server" CssClass="niceLabels">Procedure Training Records</asp:Label>
                    <asp:GridView ID="GridView2" runat="server" DataKeyNames="badge_id" 
                     BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
                     DataSourceID="SqlDataSource3" ForeColor="Black" GridLines="Vertical" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound">
                        <AlternatingRowStyle BackColor="#CCCCCC" />
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                     </asp:GridView>
                </div>
                <div class="row">&nbsp;</div>
                <div class="row" style="background-color:#FFFFFF">
                     <%-- NONPROCEDURE CLASSES VIEW --%>
                    <asp:Label ID="NonprocTraining" runat="server" CssClass="niceLabels">NonProcedure Training Records</asp:Label>
                    <asp:GridView ID="GridView3" runat="server" DataKeyNames="badge_id" 
                     BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
                     DataSourceID="SqlDataSource4" ForeColor="Black" GridLines="Vertical" AllowPaging="True">
                        <AlternatingRowStyle BackColor="#CCCCCC" />
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>
                </div>
            </div>
        </div>
    </div>
<p>
    ///  various <asp:SqlDataSources, etc... 
</asp:Content>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CPITraining
{
    public partial class Junk2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (GridView1.SelectedRow != null)
            {
                //when DETAILS are up
                DetailsView1.Visible = true;
                GridView1.Visible = false;
                EmplList.Visible = false;
                EmplHist.Visible = true;
                ProcTraining.Visible = true;
                NonprocTraining.Visible = true;
            }
            else
            {
                //when MAIN list is up
                DetailsView1.Visible = false;
                GridView1.Visible = true;
                EmplList.Visible = true;
                EmplHist.Visible = false;
                ProcTraining.Visible = false;
                NonprocTraining.Visible = false;
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string TrainingDBConnection = ConfigurationManager.ConnectionStrings["TrainingDBConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(TrainingDBConnection))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                StringBuilder sbCommand = new StringBuilder("Select * from blah, blah, blah.... where 1 = 1");
                if (txtBadge.Value.Trim != "")
                {
                    sbCommand.Append(" AND BADGE_ID=@txtBadge");
                    SqlParameter param = new SqlParameter("@txtBadge", txtBadge.Value);
                    cmd.Parameters.Add(param);
                }
                if (txtLast.Value.Trim != "")
                {
                    sbCommand.Append(" AND LASTNAME=@txtLast");
                    SqlParameter param = new SqlParameter("@txtLast", txtBadge.Value);
                    cmd.Parameters.Add(param);
                }
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSourceID = null;   //  <--- this didn't work
                GridView1.DataSource = rdr;
                GridView1.DataBind();
            }
        }
    }
}

感谢您的帮助!

c# asp.net gridview datasource
1个回答
0
投票

我知道了这一点,并在正确的轨道上:

GridView1.DataSourceID = null;
GridView1.DataSource = //... my select statement;
GridView1.DataBind();

您不能在绑定到不支持分页的数据源(基本SELECT语句)的GridView中进行分页。一旦我关闭GridView1分页,搜索就可以与同时定义的DataSourceID和DataSource完美配合。]

© www.soinside.com 2019 - 2024. All rights reserved.