using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SelectScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getdata();
            
        }
    }
    protected void getdata()
    {
        SqlConnection cn = new SqlConnection(Class1.connectionString);
        try
        {
            //调用数据库中的一个存储过程
            string sql_select = "show_score";
            SqlCommand cmd = new SqlCommand(sql_select, cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            cn.Close();
            cmd.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());            return;
        }
        finally
        {
            cn.Close();
        }    }
    protected DataSet getdataset(string sql_select, DataSet ds)
    {
        SqlConnection conn = new SqlConnection(Class1.connectionString);
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql_select, conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }
   
    //实现GrideView分页功能
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        getdata();
    }    protected void btnUpdateScore_Click(object sender, EventArgs e)
    {
        Response.Redirect("ScoreManage.aspx");
    }
    protected void ddlSelectMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sql="";
        ddlCondition.Visible = true;
        DataSet ds = new DataSet();
        //设置dropdownlist控件绑定查询条件字段
        if (ddlSelectMethod.SelectedIndex == 1)
        {
            lblCondition.Text = "请选择班级";
            sql = "Select ClassID,Desn from Class";            ddlCondition.DataValueField = "ClassID";
            ddlCondition.DataTextField = "Desn";
        }
        if (ddlSelectMethod.SelectedIndex == 2)
        {
            lblCondition.Text = "请选择年级";
            sql = "Select GradeID,Desn from Grade";
            ddlCondition.DataValueField = "GradeID";
            ddlCondition.DataTextField = "Desn";
        }
        if (ddlSelectMethod.SelectedIndex == 3)
        {
            lblCondition.Text = "请选择课程";
            sql = "select CourseID,Desn from Course";
            ddlCondition.DataValueField = "CourseID";
            ddlCondition.DataTextField = "Desn";
        }
        ddlCondition.DataSource = getdataset(sql, ds);
        ddlCondition.DataBind();
    }
    //获取新数据源
    protected void selectscore(string select_score)
    {
        SqlConnection cn = new SqlConnection(Class1.connectionString);        SqlCommand cmd = new SqlCommand(select_score, cn);
        cmd.Connection = cn;
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    //显示符合条件记录行
    protected void btnSelectScore_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = null;
        
        SqlParameter sp;
        SqlConnection cn = new SqlConnection(Class1.connectionString);
        try
        {
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;            if (ddlSelectMethod.SelectedIndex == 0)
            {
                Response.Write("<script>alert('请选择查询条件!')<script>");            }
            if (ddlCondition.SelectedIndex == 0)
            {
                Response.Write("<script>alert('请选择要查询的" + ddlSelectMethod.SelectedItem.Text + "!')<script>");            }
            if (ddlSelectMethod.SelectedIndex == 1)
            {
                cmd.CommandText = "show_scorebyclass";
                sp = new SqlParameter("@classid", SqlDbType.Int);                cmd.CommandType = CommandType.StoredProcedure;                sp.Value = Convert.ToInt16(ddlCondition.SelectedValue);                cmd.Parameters.Add(sp);
            }
            if (ddlSelectMethod.SelectedIndex == 2)
            {
                cmd.CommandText = "show_scorebygrade";
                cmd.CommandType = CommandType.StoredProcedure;
                sp = new SqlParameter("@gradeid", SqlDbType.Int);
                sp.Value = Convert.ToInt16(ddlCondition.SelectedValue);
                cmd.Parameters.Add(sp);
            }
            if (ddlSelectMethod.SelectedIndex == 3)
            {
                cmd.CommandText = "show_scorebycourse";
                cmd.CommandType = CommandType.StoredProcedure;
                sp = new SqlParameter("@courseid", SqlDbType.Int);
                sp.Value = Convert.ToInt16(ddlCondition.SelectedValue);                cmd.Parameters.Add(sp);
            }            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write("错误:" + ex.Message);
            return;
        }
        
    }
}
___________________________________________________________________________________________
,
我想用一个GridView实现成绩的查询,分别可以按班级,课程,年级查询!这个功能基本实现了,但是为什么只能查询一下,当我再次选择查询条件时,就不没有反应了,且会报错,缺少对象!到底是怎么是回事啊?谁能帮我解释一下这是为什么?最好能详细点儿!谢谢

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SelectScore.aspx.cs" Inherits="SelectScore" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>查询成绩</title>
    </head>
    <body bgcolor="#ffffff">
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" CellPadding="4"
                ForeColor="#333333" GridLines="None" Width="100%" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"  PageSize="6" Caption="查询成绩">
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
                <EditRowStyle BackColor="#999999" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:TemplateField HeaderText="通过审核">
                    <ItemTemplate>
                            <asp:CheckBox ID="chkCheck" runat="server"  ></asp:CheckBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        
        </div>
            <table style="width: 613px; height: 114px">
            <tr><td style="width: 210px">
                &nbsp; &nbsp; &nbsp;</td><td style="width: 227px">
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                选择查询条件:</td><td style="width: 244px">
                <asp:DropDownList ID="ddlSelectMethod" runat="server" Width="165px" BackColor="Gray" OnSelectedIndexChanged="ddlSelectMethod_SelectedIndexChanged" AutoPostBack="True">
                    <asp:ListItem>未选</asp:ListItem>
                    <asp:ListItem>班级</asp:ListItem>
                    <asp:ListItem>年级</asp:ListItem>
                    <asp:ListItem>课程</asp:ListItem>
                </asp:DropDownList></td></tr>
                <tr>
                    <td style="width: 210px; height: 17px;">
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                        <asp:Label ID="lblCondition" runat="server" Width="63px" Height="19px"></asp:Label>
                        &nbsp; &nbsp; &nbsp; &nbsp; :</td>
                    <td style="width: 227px; height: 17px;">
                        <asp:DropDownList ID="ddlCondition" runat="server" Width="165px" BackColor="Gray" Font-Names="Arial Black" ToolTip="请选择学号!" Visible="False">
                        </asp:DropDownList></td>
                    <td style="width: 244px; height: 17px;">
                        <asp:Button ID="btnUpdateScore" runat="server" OnClick="btnUpdateScore_Click" Text="修改成绩" Width="157px" /></td>
                </tr>
                <tr>
                    <td style="width: 210px">
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                        &nbsp; &nbsp; &nbsp;&nbsp;
                    </td>
                    <td style="width: 227px">
                        </td>
                    <td style="width: 244px">
                        </td>
                </tr>
                <tr>
                    <td style="width: 210px; height: 10px;">
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                        &nbsp; &nbsp; &nbsp;&nbsp;
                    </td>
                    <td style="width: 227px; height: 10px;">
                        </td>
                    <td style="width: 244px; height: 10px;">
                    </td>
                </tr>
                <tr> </tr>
            </table>
            &nbsp; &nbsp;<asp:Button ID="btnSelectScore" runat="server" Text="查询成绩" Width="182px" OnClick="btnSelectScore_Click" />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        </form>
    </body>
    </html>
    这是前台代码!
      

  2.   

    if(!IsPostBack)
    BindData();
    private void BindData()
    {
    //根据查询条件查询数据绑定
    }
      

  3.   

    protected void ddlSelectMethod_SelectedIndexChanged(object sender, EventArgs e)
    就是这个方法不能重复执行,只能执行一次!
      

  4.   

    在绑定数据源的方法getdata()里面把DropDownList.SelectedValue作为数据源生成的参数带入,在SelectedIndexChanged里面重新绑定绑定下,调用getdata()方法
      

  5.   

    protected void  ddlSelectMethod_SelectedIndexChanged(object sender, EventArgs e)
    就是dropdownlist的SelectedIndexChanged一个事件!每次选择不同的项就可以使另一个dropdownlist绑定不同的字段!第一次可以实现,但当我按完查询按钮,成绩出来以后,那个事件就不能用了!不知道你能明白不?是有点儿乱!