这是一本书里的新闻发布的例子,前台页面显示新闻,分为"内部新闻"和"外部新闻",可以通过选项来显示,(打开时,缺省为显示"内部新闻")此部分源码如下:
                  <td width="34%"> 
                      <div align="center"><asp:DropDownList id="DropDownList2"  runat="server"/> 
                        <asp:Button id="Button3"  Text="确定"  class="input1" Width="50" Height="19" runat="server"/></div>
                    </td>我想把选择项去掉,页面上只显示"内部新闻"的内容,但是把上面的源码删掉后,就会报错.
我是初学者,不会改,下面是后台的源码,请问后台如何改,才不会报错啊?using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
namespace www
{
public class MyCodeBehind : Page 

public DataGrid     MyList; 
        public DropDownList DropDownList1; 
public DropDownList DropDownList2; 
      
public TextBox   TextBox1;
public LinkButton   btnFirst; 
public LinkButton   btnPrev; 
public LinkButton   btnNext; 
public LinkButton   btnLast; 
public Label     lblCurrentPage; 
public Label     lblPageCount;
public Label     lblRecordCount;
public TextBox    txtIndex; public int PageCount,RecordCount;  
 
public void SubmitBtn_Click(Object sender, EventArgs e) 
{
       
{ SqlConnection myConnection = new SqlConnection("server=LIUYANG; uid=sa; pwd=; database=news");   
SqlDataAdapter myCommand = new  SqlDataAdapter("select * from aa where " + DropDownList1.SelectedItem.Value + "  like '%" + TextBox1.Text.ToString() + "%'", myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "tt"); MyList.DataSource = ds.Tables["tt"].DefaultView;
MyList.DataBind();
}
} private void Page_Load(Object sender, EventArgs e)    
{    
if(!IsPostBack) 
{
SqlConnection MyConnection =new SqlConnection("server=LIUYANG; uid=sa; pwd=; database=news");  
SqlDataAdapter myCommand=new SqlDataAdapter("SELECT id,typename FROM bb ",MyConnection);   
DataSet ds= new DataSet();  
myCommand.Fill(ds,"bb");  
DropDownList2.DataSource = ds.Tables["bb"].DefaultView;    
DropDownList2.DataTextField = "typename";  
DropDownList2.DataValueField = "id";  
DropDownList2.DataBind(); 

}
  DataBind(); 
}    
          
      
DataView CreateDataSource()   
{   
SqlConnection myConnection = new SqlConnection("server=LIUYANG; uid=sa; pwd=; database=news");       
SqlDataAdapter myCommand = new  SqlDataAdapter("select  * from aa WHERE typeid="+ DropDownList2.SelectedItem.Value+"", myConnection);    
              
DataSet ds = new DataSet();    
myCommand.Fill(ds, "aa");     return ds.Tables["aa"].DefaultView;    
}    void DataBind()   
{   
DataView source=CreateDataSource();   
if(!IsPostBack)   
{   
RecordCount=source.Count;   
PageCount=RecordCount/MyList.PageSize;   
if((RecordCount%MyList.PageSize)!=0) PageCount++;
lblRecordCount.Text=RecordCount.ToString();  
lblPageCount.Text=PageCount.ToString();  
lblCurrentPage.Text="1"; 
}   
MyList.DataSource = source;    
MyList.DataBind();    
}    public void MyList_Page(Object sender, DataGridPageChangedEventArgs e) 
{    
//由内置页导航使用。CurrentPageIndex 已设置    
MyList.CurrentPageIndex = e.NewPageIndex;    
DataBind();    
}       
        
public void txtIndex_Changed(Object sender, EventArgs e) 
{    
         
btnFirst.Enabled=true; 
btnPrev.Enabled=true; 
btnNext.Enabled=true; 
btnLast.Enabled=true; 
           
int index=Int32.Parse(txtIndex.Text.ToString());     
PageCount=Int32.Parse(lblPageCount.Text.ToString()); 
if(index>=1&&index<=PageCount)     
{     
MyList.CurrentPageIndex=index-1;      
DataBind();   
lblCurrentPage.Text=index.ToString(); 

if(index==1) 
{
btnFirst.Enabled=false; 
btnPrev.Enabled=false; 

else if(index==PageCount) 
{
btnLast.Enabled=false; 
btnNext.Enabled=false; 

        
           
else 

{ txtIndex.Text=""; } 
DataBind();
}       
}     
        
public void PagerButtonClick(Object sender, CommandEventArgs e)     
{    
btnFirst.Enabled=true; 
btnPrev.Enabled=true; 
btnNext.Enabled=true; 
btnLast.Enabled=true;  //由外部分页 UI 使用    
String arg = e.CommandArgument.ToString();     PageCount=Int32.Parse(lblPageCount.Text.ToString());  
int pageindex=Int32.Parse(lblCurrentPage.Text.ToString())-1;  switch(arg)    
{    
case "Next":    
if (pageindex < (PageCount - 1))    
pageindex ++;    
break;    
case "Prev":    
if (pageindex > 0)    
pageindex --;    
break;    
case "Last":    
pageindex = (PageCount - 1);    
break;  
case "First":  
pageindex=0;  
break;  
}    
if(pageindex==0) 
{
btnFirst.Enabled=false; 
btnPrev.Enabled=false; 

else if(pageindex==PageCount-1) 
{
btnLast.Enabled=false; 
btnNext.Enabled=false; 

MyList.CurrentPageIndex=pageindex; 
DataBind();    
lblCurrentPage.Text=(MyList.CurrentPageIndex+1).ToString();  

}  
    protected string FormatString(string str) 

str=str.Replace(" ","&nbsp;&nbsp;"); 
str=str.Replace("<","&lt;"); 
str=str.Replace(">","&gt;"); 
str=str.Replace('\n'.ToString(),"<br>"); 
return str; 


}

解决方案 »

  1.   

    把("select  * from aa WHERE typeid="+ DropDownList2.SelectedItem.Value+"", myConnection); 
    中的 "+ DropDownList2.SelectedItem.Value+" 去掉,改为对应的内部新闻的ID就可以了 
      

  2.   

    不行啊,出错了   (Line 53为红色显示)
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error: 
    Line 51:  DataSet ds= new DataSet();  
    Line 52:  myCommand.Fill(ds,"bb");    
    Line 53:  DropDownList2.DataSource = ds.Tables["bb"].DefaultView;    
    Line 54:  DropDownList2.DataTextField = "typename";  
    Line 55:  DropDownList2.DataValueField = "id";  
     
      

  3.   

    嗯,这个程序我看到过,正如楼上所说,你的sql中用到了,这个控间的DropDownList2得SelectedItem.Value属性,所以你需要把这个一楼所说的地方改掉,
      

  4.   

    //DropDownList2.DataSource = ds.Tables["bb"].DefaultView;    
    //DropDownList2.DataTextField = "typename";  
    //DropDownList2.DataValueField = "id";  
    //DropDownList2.DataBind(); 
    将你代码中的这个地方,像上面的样子给屏蔽掉,在看看
      

  5.   

    呵呵,把DropDownList2出现的地方都注释掉就可以了
      

  6.   

    你已经把DropDownList2与Button3那么你后台代码里就就要把所有DropDownList2与Button3的代码都删除