当前位置:编程学习 > C#/ASP.NET >>

如何选择DropDownList1中的值,在gridview中显示相应的内容?比如我选择171,显示171班学生信息。

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;

using System.Data.OleDb;

 


public partial class shouye : System.Web.UI.Page

{

    //string connStr = ConfigurationManager.ConnectionStrings["XSKGLXTConnectionString"].ConnectionString;

    string strConn = "Data Source=(local);DataBase=XSKGLXT;persist security info=True;trusted_connection=sspi;packet size=4096"; //链接SQL数据库

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            string sql = "SELECT xsk_bh.bh  FROM xsk_bh ";

 

            SqlConnection conn1 = new SqlConnection(strConn);


            conn1.Open();

            SqlDataAdapter ad1 = new SqlDataAdapter(sql, conn1);

            DataSet ds1 = new DataSet();

            ad1.Fill(ds1);

            this.DropDownList1.DataSource = ds1;

            this.DropDownList1.DataTextField = "bh";

            this.DropDownList1.DataValueField = "bh";

            this.DropDownList1.DataBind();

            if (conn1.State == ConnectionState.Open)

            {

                conn1.Close();

            }

           

        }

        

    }

 


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        GridView1.PageIndex = e.NewPageIndex;

        GridView1.DataBind();

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

 

    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        string str = this.DropDownList1.SelectedValue.ToString();

 

         if (str == "171")
        {


            //string connStr = ConfigurationManager.ConnectionStrings["XSKGLXTConnectionString"].ConnectionString;

            SqlConnection conn = new SqlConnection(strConn);

            conn.Open();

            SqlDataAdapter ad = new SqlDataAdapter("SELECT xsk_171.xh as 学号,xsk_171.xm as 姓名, xsk_171.xb as 性别 FROM xsk_171,xsk_bh where xsk_171.bh=xsk_bh.bh ", conn);

            DataSet ds = new DataSet();

            ad.Fill(ds);

            

            this.GridView1.DataSource = ds;

            this.GridView1.DataBind();

 

            if (conn.State == ConnectionState.Open)


            {

                conn.Close();

            }

        

    }

}

我现在选择了171,但是gridview显示不出来内容。

 171是在数据表中的班号这个字段,绑定在DropDownList1上。

 



gridview --------------------编程问答-------------------- page_load又重新刷新了吧 --------------------编程问答--------------------
引用 1 楼 gxingmin 的回复:
page_load又重新刷新了吧


你在PageLoad里就要将dropdrownList里的值作为参数传入SQL,默认为空则显示所有数据,选择了才显示指定内容 --------------------编程问答-------------------- 你既然做了IsPostBack的判断,为什么不把选中的值在这个里面进行取值 --------------------编程问答--------------------
在点击的时候重新查询绑定。
点击传入绑定的ID
补充:.NET技术 ,  组件/控件开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,