各位高手,紧急求助,一定给分!!!
为什么以下代码运行后,GRIDVIEW控件显示不出来!!!!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;
public partial class Hyxt_UserLook : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}
DataBase.DBFactory clsDbFac = new DataBase.DBFactory();
//根据会员名称查询数据,并判断是否有此会员名称
protected void Button1_Click(object sender, EventArgs e)
{
string sql1 = "select count(*) from [USER] where USERNAME='" + this.txtUserName.Text+ "'";
string st = clsDbFac.ExecuteSqlScalar(sql1);
int intReturn = Convert.ToInt32(st);
if (intReturn > 0)
{
DataBind();
}
}
//重写绑定方法
public override void DataBind()
{
string sql2 = "select * from [USER] where USERNAME='" + this.txtUserName.Text + "'"; ;
DataTable dt = clsDbFac.ExecuteSqlDataTable(sql2);
this.gdvUser.DataSource = dt;
this.gdvUser.DataKeyNames = new string[] { "USERID" };
this.gdvUser.DataBind();
}
//删除会员的方法
protected void gdvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
//int id= Convert.ToInt32(gdvUser.DataKeys[e.RowIndex].Value.ToString());
string userid = gdvUser.DataKeys[e.RowIndex].Value.ToString();
string sql = "delete from [USER] where ID = " + userid + "";
int i = clsDbFac.ExecuteSqlNonQuery(sql);
DataBind();
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
}
}
为什么以上代码运行后,GRIDVIEW控件显示不出来!!!! --------------------编程问答-------------------- 1.DataBase.DBFactory clsDbFac = new DataBase.DBFactory(); 这个不要这样声明,WEB不同C/S模式,不然你点删除时,肯定会出错的.把它放到各个方法里,不要声明为整个类的.
2.string st = clsDbFac.ExecuteSqlScalar(sql1); //?你这里是什么意思?这个方法返回的应该是结果里的第一行第一列的"值",而不是行数
int intReturn = Convert.ToInt32(st); //同上
if (intReturn > 0) //我估计你的这个条件块都没运行
{
DataBind();
}
if (!Page.IsPostBack) //这里说明是第一次加载页面时运行的,但看了一下你数据绑定的方法,有错误
{
DataBind();
}
//重写绑定方法
public override void DataBind()
{
string sql2 = "select * from [USER] where USERNAME='" + this.txtUserName.Text + "'"; ; //????你的这个文本框是哪来的,如果你的网页第一次加载时,肯定取不到数据的,人家根本就没有在文本框里输入值(还没打开怎么输入?)
DataTable dt = clsDbFac.ExecuteSqlDataTable(sql2);
this.gdvUser.DataSource = dt;
this.gdvUser.DataKeyNames = new string[] { "USERID" };
this.gdvUser.DataBind();
}
--------------------编程问答-------------------- 1. DataBase.DBFactory clsDbFac = new DataBase.DBFactory(); 這個放在
public override void DataBind()
{
DataBase.DBFactory clsDbFac = new DataBase.DBFactory();
string sql2 = "select * from [USER] where USERNAME='" + this.txtUserName.Text + "'"; ;
DataTable dt = clsDbFac.ExecuteSqlDataTable(sql2);
this.gdvUser.DataSource = dt;
this.gdvUser.DataKeyNames = new string[] { "USERID" };
this.gdvUser.DataBind();
}
2. SQL查詢條件是否符合!先去掉查詢條件試試!
补充:.NET技术 , ASP.NET