.最近在做毕业设计,遇到很多问题,都可以解决,但这两天遇到两个问题一直解决不了.
首先是GridView(GV)选择查询后绑定数据,不能正确编辑和分页,我反复查看,知道编辑会出错是问题错在row index,因为查询后绑定GV当编辑时,index为默认显示的index,
但问题就在:怎么绑定才会解决?
还有选择查询后,分页也解决不了,会回到原来所有数据...高手帮帮忙..下面是代码:
后台:
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 bookclass_admin : System.Web.UI.Page
{ private static int _selectedID; //当前行的ID,全局变量
public static int SelectedID
{
get { return _selectedID; }
set { _selectedID = value; }
}
private static string _strAClassName; //定义strAClassName为全局变量
public static string StrAClassName
{
get { return _strAClassName; }
set { _strAClassName = value; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//显示B子类
string strsql = "select * from bclass"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass";
GridView2.DataBind();
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
bookclass_admin._selectedID = (int)GridView1.DataKeys[e.NewSelectedIndex].Value; //获得选择的A类的ID //打开数据库得表AClass的AClassName
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
string sqlGetAname = "select AClassName from AClass where AClassId=" + Convert.ToInt32(bookclass_admin._selectedID);
SqlCommand cmdtmp = new SqlCommand(sqlGetAname, conntmp);
bookclass_admin.StrAClassName = (string)cmdtmp.ExecuteScalar();
conntmp.Close(); //显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; pnl_addBclass.Visible = false; //添加B类子类的面板不可见
}
protected void btnAddCls_Click(object sender, EventArgs e)
{
//打开数据库AClass增加A类
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsqladd = "insert into aclass (aclassname) values ('" + this.txtAddClass.Text + "')";
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
SqlCommand cmdtmp = new SqlCommand(strsqladd,conntmp);
cmdtmp.ExecuteNonQuery();
conntmp.Close(); GridView1.DataSourceID = "SqlDataSource_Aclass"; //重新绑定datasource实现刷新 this.txtAddClass.Text = " "; }
protected void btnCan_Click(object sender, EventArgs e)
{
//手动清空文本框
this.txtAddClass.Text = " ";
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnJoin")
{
int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
bookclass_admin._selectedID = (int)GridView1.DataKeys[rowIndex].Value; //行的id赋值给selectedID
//打开数据库得表AClass的AClassName
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
string sqlGetAname = "select AClassName from AClass where AClassId=" + Convert.ToInt32(bookclass_admin._selectedID);
SqlCommand cmdtmp = new SqlCommand(sqlGetAname, conntmp);
bookclass_admin.StrAClassName = (string)cmdtmp.ExecuteScalar(); //全局strAClassName , btnAddBclass_y_Click()也用到
conntmp.Close(); this.blAddBClass.Text = bookclass_admin.StrAClassName; //显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; pnl_addBclass.Visible = true;
}
}
protected void btnAddBclass_y_Click(object sender, EventArgs e)
{ //打开数据库得表BClass增加子类
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsqladd = "insert into BClass (BClassName,AClassName) values ('" + this.txtAddBclass.Text + "','" + bookclass_admin.StrAClassName + "')";
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
SqlCommand cmdtmp = new SqlCommand(strsqladd, conntmp);
cmdtmp.ExecuteNonQuery();
conntmp.Close();
//显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; //重新绑定datasource实现刷新 this.txtAddBclass.Text = " ";
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataBind();
}
}
前台:
首先是GridView(GV)选择查询后绑定数据,不能正确编辑和分页,我反复查看,知道编辑会出错是问题错在row index,因为查询后绑定GV当编辑时,index为默认显示的index,
但问题就在:怎么绑定才会解决?
还有选择查询后,分页也解决不了,会回到原来所有数据...高手帮帮忙..下面是代码:
后台:
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 bookclass_admin : System.Web.UI.Page
{ private static int _selectedID; //当前行的ID,全局变量
public static int SelectedID
{
get { return _selectedID; }
set { _selectedID = value; }
}
private static string _strAClassName; //定义strAClassName为全局变量
public static string StrAClassName
{
get { return _strAClassName; }
set { _strAClassName = value; } }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//显示B子类
string strsql = "select * from bclass"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass";
GridView2.DataBind();
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
bookclass_admin._selectedID = (int)GridView1.DataKeys[e.NewSelectedIndex].Value; //获得选择的A类的ID //打开数据库得表AClass的AClassName
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
string sqlGetAname = "select AClassName from AClass where AClassId=" + Convert.ToInt32(bookclass_admin._selectedID);
SqlCommand cmdtmp = new SqlCommand(sqlGetAname, conntmp);
bookclass_admin.StrAClassName = (string)cmdtmp.ExecuteScalar();
conntmp.Close(); //显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; pnl_addBclass.Visible = false; //添加B类子类的面板不可见
}
protected void btnAddCls_Click(object sender, EventArgs e)
{
//打开数据库AClass增加A类
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsqladd = "insert into aclass (aclassname) values ('" + this.txtAddClass.Text + "')";
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
SqlCommand cmdtmp = new SqlCommand(strsqladd,conntmp);
cmdtmp.ExecuteNonQuery();
conntmp.Close(); GridView1.DataSourceID = "SqlDataSource_Aclass"; //重新绑定datasource实现刷新 this.txtAddClass.Text = " "; }
protected void btnCan_Click(object sender, EventArgs e)
{
//手动清空文本框
this.txtAddClass.Text = " ";
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnJoin")
{
int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
bookclass_admin._selectedID = (int)GridView1.DataKeys[rowIndex].Value; //行的id赋值给selectedID
//打开数据库得表AClass的AClassName
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
string sqlGetAname = "select AClassName from AClass where AClassId=" + Convert.ToInt32(bookclass_admin._selectedID);
SqlCommand cmdtmp = new SqlCommand(sqlGetAname, conntmp);
bookclass_admin.StrAClassName = (string)cmdtmp.ExecuteScalar(); //全局strAClassName , btnAddBclass_y_Click()也用到
conntmp.Close(); this.blAddBClass.Text = bookclass_admin.StrAClassName; //显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; pnl_addBclass.Visible = true;
}
}
protected void btnAddBclass_y_Click(object sender, EventArgs e)
{ //打开数据库得表BClass增加子类
string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsqladd = "insert into BClass (BClassName,AClassName) values ('" + this.txtAddBclass.Text + "','" + bookclass_admin.StrAClassName + "')";
SqlConnection conntmp = new SqlConnection(strconn);
conntmp.Open();
SqlCommand cmdtmp = new SqlCommand(strsqladd, conntmp);
cmdtmp.ExecuteNonQuery();
conntmp.Close();
//显示B子类
//string strconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string strsql = "select * from bclass where AClassName='" + bookclass_admin.StrAClassName + "'"; //加入选中的值
SqlDataSource_BClass.SelectCommand = strsql;
GridView2.DataSourceID = "SqlDataSource_BClass"; //重新绑定datasource实现刷新 this.txtAddBclass.Text = " ";
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataBind();
}
}
前台:
谢了