问题描述
下面是Page_Load的代码,我改了好多遍了chooseclassid不给值第一次执行时是对的,页面提交后应该还是没有给值的,却执行到了给SEARCHtext复了个值,请问是什么问题啊!privatevoidPage_Load(objectsender,System.EventArgse){OleDbConnectionconn=db.createConnection();conn.Open();if(!IsPostBack){OleDbCommandsql=newOleDbCommand("select*fromclass_neworderbyclassidasc",conn);OleDbDataReaderrs=sql.ExecuteReader();while(rs.Read()){if(Convert.ToString(rs["classid"]).Length/5!=1){intmaxi=Convert.ToString(rs["classid"]).Length/5;stringclasslv="";for(inti=1;i<=maxi-1;i++){classlv=classlv+"—";}classid.Items.Add(newListItem(Convert.ToString(classlv+rs["classname"]),Convert.ToString(rs["orderid"])));}else{classid.Items.Add(newListItem(Convert.ToString(rs["classname"]),Convert.ToString(rs["orderid"])));}}rs.Close();}stringkey=Convert.ToString(Request["key"]);stringchooseclassid=Convert.ToString(Request["classid"]);stringsearchtext=string.Empty;stringallclassid=string.Empty;if(key!=""&&key!=null){searchtext="andtitlelike'%"+key+"%'";}if(chooseclassid!=""&&chooseclassid!=null){OleDbCommands1=newOleDbCommand("select*fromclass_newwhereclassidlike'"+chooseclassid+"%'",conn);OleDbDataReaders2=s1.ExecuteReader();while(s2.Read()){allclassid=allclassid+Convert.ToInt32(s2["orderid"])+",";}searchtext=searchtext+"andclassidin("+allclassid+")";s2.Close();}OleDbDataAdapterMyDataAdapter=newOleDbDataAdapter("select*fromnewswhere1=1"+searchtext+"orderbyinfotimedesc",conn);DataSetMyDataSet=newDataSet();MyDataAdapter.Fill(MyDataSet,"news");dgNews.DataSource=MyDataSet.Tables["news"].DefaultView;dgNews.DataBind();}
解决方案
解决方案二:
你把下面的代码都放到if(!ispostback)里试试