如何选择DropDownList1中的值,在gridview中显示相应的内容?比如我选择171,显示171班学生信息。 5
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
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上。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = this.DropDownList1.SelectedValue.ToString();
if (str == "171")
中的这条if语句该怎么写呢? 展开
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上。
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = this.DropDownList1.SelectedValue.ToString();
if (str == "171")
中的这条if语句该怎么写呢? 展开
2个回答
展开全部
先检查dropdownlist的autopostback属性是否为true,然后加断点,取出sql语句放到sql server里查询是否有返回值,如果在sql server里有返回值,则检查gridview绑定是否出错
if (str == "171")
{
//这里可以不用再次连接数据库了
string sql=string.format("SELECT xsk_{0}.xh as 学号,xsk_{0}.xm as 姓名, xsk_{0}.xb as 性别 FROM xsk_{0},xsk_bh where xsk_{0}.bh=xsk_bh.bh ",str);
SqlDataAdapter ad1 = new SqlDataAdapter(sql, conn1);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
if (str == "171")
{
//这里可以不用再次连接数据库了
string sql=string.format("SELECT xsk_{0}.xh as 学号,xsk_{0}.xm as 姓名, xsk_{0}.xb as 性别 FROM xsk_{0},xsk_bh where xsk_{0}.bh=xsk_bh.bh ",str);
SqlDataAdapter ad1 = new SqlDataAdapter(sql, conn1);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询