我用一串数组格式组成的字符串,作为参数传递给页面a.aspx,在这个页面中,将它转化成数组arrselected[].用里面的数据项作为数据绑定的记录的ID号,依次绑定.但是在我对其中的某条记录做删除操作后,想重新帮定,发生了错误 代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
pageSize = 5;
if(!Page.IsPostBack)
{
currentPage = 0; // 初始化当前页为第0页
ViewState["CurrentPage"] = 0; selectedstring=Request.QueryString["ID"];
//selectedstringTemp=selectedstring;
arrselected = selectedstring.Split(',');
ViewState["arrselected"]=arrselected;}
public void BindData()
{
int startPage;
startPage = currentPage * pageSize;
//查询语句
string selectStr="select reason,id,hr_depart.dptname,hr_overtimetemp.id,hr_emply.emplyname,hr_overtimekind.overtimename,bgndatetime ,enddatetime ,hr_check.checkname,hr_overtimetemp.checked,sendtime,result from hr_overtimekind,hr_overtimetemp,hr_depart,hr_check,hr_emply where hr_check.checked=hr_overtimetemp.checked and hr_depart.dptid=hr_overtimetemp.dptid and hr_overtimekind.overtimekind=hr_overtimetemp.overtimekindid and hr_emply.emplyid=hr_overtimetemp.emplyid and id in (";
//循环获得数组里的数据
for(int i=0;i<arrselected.Length;i++)
{
if(arrselected[i].Trim()!="")
{
selectStr+="'"+arrselected[i]+"',";
}
}
//去掉最后一个逗号
selectStr = selectStr.Substring(0,selectStr.Length-1);
selectStr+=") ";
DataSet ds = new DataSet();
hr.conn.Open();
SqlDataAdapter da = new SqlDataAdapter(selectStr,hr.conn);
da.Fill(ds,startPage,pageSize,"Message");
message.DataSource = ds;
message.DataMember = "Message";
message.DataBind();
hr.conn.Close();
}
public void DelOrAlt_Click(object sender,CommandEventArgs e)
{
string commandText; bool result=true;
if(e.CommandName=="Del")
{
try
{
commandText = "Delete from hr_overtimetemp where id = " + e.CommandArgument;
hr.conn.Open();
SqlCommand comm = new SqlCommand(commandText,hr.conn);
comm.ExecuteNonQuery();
hr.conn.Close();
Response.Write("<script>alert(\"删除成功!\");</script>");
int Del=int.Parse(e.CommandArgument.ToString());
arrselected=ViewState["arrselected"].ToString;
for(int i=0;i<arrselected.Length;i++)
{
if(arrselected[i].Trim()!="")
{
if(Del==int.Parse(arrselected[i].ToString()))
{
break; }
}
j++;//记录被删除的数据在数组里的位置
}
//删除数组里的那个被已经被删除的记录对应的值
for(int i=j;i<arrselected.Length-1;i++)
{
arrselected[i]=arrselected[i+1];
}
BindData();
}
catch(SqlException)
{
Response.Write("<script>alert(\"删除失败!\");</script>");
result=false;
} }}发生的错误是::for(int i=j;i<arrselected.Length-1;i++)
{
arrselected[i]=arrselected[i+1];
}
上句里说arrselected未将对象实例化
请各位大虾帮忙啊
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
pageSize = 5;
if(!Page.IsPostBack)
{
currentPage = 0; // 初始化当前页为第0页
ViewState["CurrentPage"] = 0; selectedstring=Request.QueryString["ID"];
//selectedstringTemp=selectedstring;
arrselected = selectedstring.Split(',');
ViewState["arrselected"]=arrselected;}
public void BindData()
{
int startPage;
startPage = currentPage * pageSize;
//查询语句
string selectStr="select reason,id,hr_depart.dptname,hr_overtimetemp.id,hr_emply.emplyname,hr_overtimekind.overtimename,bgndatetime ,enddatetime ,hr_check.checkname,hr_overtimetemp.checked,sendtime,result from hr_overtimekind,hr_overtimetemp,hr_depart,hr_check,hr_emply where hr_check.checked=hr_overtimetemp.checked and hr_depart.dptid=hr_overtimetemp.dptid and hr_overtimekind.overtimekind=hr_overtimetemp.overtimekindid and hr_emply.emplyid=hr_overtimetemp.emplyid and id in (";
//循环获得数组里的数据
for(int i=0;i<arrselected.Length;i++)
{
if(arrselected[i].Trim()!="")
{
selectStr+="'"+arrselected[i]+"',";
}
}
//去掉最后一个逗号
selectStr = selectStr.Substring(0,selectStr.Length-1);
selectStr+=") ";
DataSet ds = new DataSet();
hr.conn.Open();
SqlDataAdapter da = new SqlDataAdapter(selectStr,hr.conn);
da.Fill(ds,startPage,pageSize,"Message");
message.DataSource = ds;
message.DataMember = "Message";
message.DataBind();
hr.conn.Close();
}
public void DelOrAlt_Click(object sender,CommandEventArgs e)
{
string commandText; bool result=true;
if(e.CommandName=="Del")
{
try
{
commandText = "Delete from hr_overtimetemp where id = " + e.CommandArgument;
hr.conn.Open();
SqlCommand comm = new SqlCommand(commandText,hr.conn);
comm.ExecuteNonQuery();
hr.conn.Close();
Response.Write("<script>alert(\"删除成功!\");</script>");
int Del=int.Parse(e.CommandArgument.ToString());
arrselected=ViewState["arrselected"].ToString;
for(int i=0;i<arrselected.Length;i++)
{
if(arrselected[i].Trim()!="")
{
if(Del==int.Parse(arrselected[i].ToString()))
{
break; }
}
j++;//记录被删除的数据在数组里的位置
}
//删除数组里的那个被已经被删除的记录对应的值
for(int i=j;i<arrselected.Length-1;i++)
{
arrselected[i]=arrselected[i+1];
}
BindData();
}
catch(SqlException)
{
Response.Write("<script>alert(\"删除失败!\");</script>");
result=false;
} }}发生的错误是::for(int i=j;i<arrselected.Length-1;i++)
{
arrselected[i]=arrselected[i+1];
}
上句里说arrselected未将对象实例化
请各位大虾帮忙啊
解决方案 »
- 求助!大网站建设 求解决方案
- ajax传值乱码的问题
- 如何对一个网站象BAIDU一样的搜索一个内容,点击后显示详细
- javascript计算汇总行?
- 求孟子E章的《ASP.NET2.0应用开发技术》一书第九章的代码,谢谢
- 100,100,100,求:模拟登录抓取新闻。在线,立即给分。
- 求助:怎样在关闭IE页面时(用鼠标点X时),打开一个新页面,但原来页面不关闭且没有任何提示框
- 急:::: VS2005 日历控件如何重新触发DayRender,
- 怎么用JQuery获取两个时间之差?
- formview 怎么不能绑定?
- C#.net中高级讨论群(QQ群号:8770499),初学者免进.
- DataGrid 中如何设置整行链接,详见内!
ViewState["arrselected"]=arrselected;
我在最前面已经定义过string [] arrselected .如果将定义数组放在page_load里面 后面的函数是找不到这个数组的. 是这样子的吧