因为在DropDownList的selectedindexchanged事件中隐藏了一个回发

解决方案 »

  1.   

    谢谢,我现在已经可以实现级联选择了,但是选择几次后就出现以下错误该怎么办。
    ---------------------------------------------------------------------------------
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 91:  Cls.dbConnection=pagebase.constr; 
    行 92: // ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,name from kdata,teacher,kdatateacher,classdata WHERE kdata.kddataid=kdatateacher.kdataid and kdata.classdataid=classdata.classdataid and teacher.teacherid=kdatateacher.teacherid  and  kdata.classdataid=1"+jishuDown.SelectedItem.Value);
    行 93:  ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,teacher.name from kdata,teacher,kdatateacher,classdata WHERE kdata.kddataid=kdatateacher.kdataid and kdata.classdataid=classdata.classdataid and teacher.teacherid=kdatateacher.teacherid  and  kdata.classdataid="+jishuDown.SelectedItem.Value);
    行 94: 
    行 95:  //ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,name from kdata,teacher WHERE   classdataid=" +jishuDown.SelectedItem.Value);
     源文件: d:\院系端辅助教学系统\开课管理系统(裘文彬)\kksystem(开课管理主程序)\paitext.aspx.cs    行: 93 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       kksystem.paitext.Page_Load(Object sender, EventArgs e) in d:\院系端辅助教学系统\开课管理系统(裘文彬)\kksystem(开课管理主程序)\paitext.aspx.cs:93
       System.Web.UI.Control.OnLoad(EventArgs e)
       System.Web.UI.Control.LoadRecursive()
       System.Web.UI.Page.ProcessRequestMain()
    ----------------------------------------------------------------------------------
      

  2.   

    接上面的源代码:
    以下是源代码using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Util ;
    using System.Data.SqlClient; 
    using squishyWARE.WebComponents.squishyTREE; 
    using Microsoft.ApplicationBlocks.Data;
    using System.Configuration;
    namespace kksystem
    {
    /// <summary>
    /// paitext 的摘要说明。
    /// </summary>
    public class paitext : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid MyDataGrid2;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DropDownList jishuDown;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.DropDownList zhyDown;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.DropDownList xiDown;
    protected System.Web.UI.WebControls.DropDownList xueyDown;
    protected System.Web.UI.WebControls.Label Label2; protected DataSet ds2;
    protected SqlDataReader dxre2;

    private void Page_Load(object sender, System.EventArgs e)
    {//下拉框初始化
    if(!this.IsPostBack)
    {
    Cxydata dx=new Cxydata();

    dx.dbConnection=ConfigurationSettings.AppSettings["SQLConnectionString"];
    dxre2=dx.GetDataReader("select * from xydata");
    xueyDown.DataSource=dxre2; 
    xueyDown.DataTextField="name";
    xueyDown.DataValueField="xydataid";
    xueyDown.DataBind(); 
    dxre2.Close(); 
                   
    if (xueyDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from xdata where xydataid="+xueyDown.SelectedItem.Value.ToString());
    xiDown.DataSource=dxre2;
    xiDown.DataTextField="name";
    xiDown.DataValueField="xdataid";
    xiDown.DataBind(); 
    dxre2.Close(); 
    }
    if (xiDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from zydata where xdataid="+xiDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    zhyDown.DataTextField="name";
    zhyDown.DataValueField="zydataid";
    zhyDown.DataBind(); 
    dxre2.Close(); 
    } if (zhyDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from classdata where zydataid="+zhyDown.SelectedItem.Value.ToString());
    jishuDown.DataSource=dxre2;
    jishuDown.DataTextField="js";
    jishuDown.DataValueField="classdataid";
    jishuDown.DataBind(); 
    dxre2.Close(); 
    }

    } //datagrid 初始化 Ckdata Cls=new Ckdata();
    Cls.dbConnection=pagebase.constr; 
    // ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,name from kdata,teacher,kdatateacher,classdata WHERE kdata.kddataid=kdatateacher.kdataid and kdata.classdataid=classdata.classdataid and teacher.teacherid=kdatateacher.teacherid  and  kdata.classdataid=1"+jishuDown.SelectedItem.Value);
    ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,teacher.name from kdata,teacher,kdatateacher,classdata WHERE kdata.kddataid=kdatateacher.kdataid and kdata.classdataid=classdata.classdataid and teacher.teacherid=kdatateacher.teacherid  and  kdata.classdataid="+jishuDown.SelectedItem.Value); //ds2=Cls.GetDataSet("SELECT kcname,lbid,xz,ketime,weekxs,name from kdata,teacher WHERE   classdataid=" +jishuDown.SelectedItem.Value);

    MyDataGrid2.DataSource= ds2;
    MyDataGrid2.DataBind(); 
    ds2.Dispose();  // 在此处放置用户代码以初始化页面
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.jishuDown.SelectedIndexChanged += new System.EventHandler(this.jishuDown_SelectedIndexChanged);
    this.zhyDown.SelectedIndexChanged += new System.EventHandler(this.zhyDown_SelectedIndexChanged);
    this.xiDown.SelectedIndexChanged += new System.EventHandler(this.xiDown_SelectedIndexChanged);
    this.xueyDown.SelectedIndexChanged += new System.EventHandler(this.xueyDown_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void xueyDown_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Cxydata dx=new Cxydata();

    xiDown.Items.Clear();
    zhyDown.Items.Clear(); 
    jishuDown.Items.Clear();
    dx.dbConnection=ConfigurationSettings.AppSettings["SQLConnectionString"];
    dxre2=dx.GetDataReader("SELECT * from xdata where xydataid="+xueyDown.SelectedItem.Value.ToString());
    xiDown.DataSource=dxre2;
    xiDown.DataTextField="name";
    xiDown.DataValueField="xdataid";
    xiDown.DataBind(); 
    dxre2.Close(); 
    //           
    // if (xueyDown.SelectedIndex>=0)
    // { 
    //
    // dxre2=dx.GetDataReader("SELECT * from xdata where xydataid="+xueyDown.SelectedItem.Value.ToString());
    // xiDown.DataSource=dxre2;
    // xiDown.DataTextField="name";
    // xiDown.DataValueField="xdataid";
    // xiDown.DataBind(); 
    // dxre2.Close();
    //
    // } if (xiDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from zydata where xdataid="+xiDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    zhyDown.DataTextField="name";
    zhyDown.DataValueField="zydataid";
    zhyDown.DataBind(); 
    dxre2.Close(); 
    } if (zhyDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from classdata where zydataid="+zhyDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    jishuDown.DataTextField="js";
    jishuDown.DataValueField="classdataid";
    jishuDown.DataBind(); 
    dxre2.Close(); 
    } } private void xiDown_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Cxydata dx=new Cxydata();
    zhyDown.Items.Clear(); 
    jishuDown.Items.Clear();
    dx.dbConnection=ConfigurationSettings.AppSettings["SQLConnectionString"];       
    dxre2=dx.GetDataReader("SELECT * from zydata where xdataid="+xiDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    zhyDown.DataTextField="name";
    zhyDown.DataValueField="zydataid";
    zhyDown.DataBind(); 
    dxre2.Close(); // if (xiDown.SelectedIndex>=0)
    // { 
    // dxre2=dx.GetDataReader("SELECT * from zydata where xdataid="+xiDown.SelectedItem.Value.ToString());
    // zhyDown.DataSource=dxre2;
    // zhyDown.DataTextField="name";
    // zhyDown.DataValueField="zydataid";
    // zhyDown.DataBind(); 
    // dxre2.Close(); 
    // } if (zhyDown.SelectedIndex>=0)

    dxre2=dx.GetDataReader("SELECT * from classdata where zydataid="+zhyDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    jishuDown.DataTextField="js";
    jishuDown.DataValueField="classdataid";
    jishuDown.DataBind(); 
    dxre2.Close(); 
    }

    } private void zhyDown_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Cxydata dx=new Cxydata();
    jishuDown.Items.Clear();
    dx.dbConnection=ConfigurationSettings.AppSettings["SQLConnectionString"];       
    dxre2=dx.GetDataReader("SELECT * from classdata where zydataid="+zhyDown.SelectedItem.Value.ToString());
    zhyDown.DataSource=dxre2;
    jishuDown.DataTextField="js";
    jishuDown.DataValueField="classdataid";
    jishuDown.DataBind(); 
    dxre2.Close(); 

    } private void jishuDown_SelectedIndexChanged(object sender, System.EventArgs e)
    {

    }
    }

      

  3.   

    我的一个程序,供楼主参考。
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using CraftPortal.Components;
    using System.IO;namespace CraftPortal.Admin
    {
    /// <summary>
    /// ProductAdd 的摘要说明。
    /// </summary>
    public class ProductAdd : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button ButtonOK;
    protected System.Web.UI.WebControls.TextBox TextBoxDescriptions;
    protected System.Web.UI.WebControls.RadioButtonList RadioButtonListSellState;
    protected System.Web.UI.WebControls.RadioButtonList RadioButtonListPick;
    protected System.Web.UI.WebControls.TextBox TextBoxVIPPrice;
    protected System.Web.UI.WebControls.TextBox TextBoxMemberPrice;
    protected System.Web.UI.WebControls.TextBox TextBoxMarketPrice;
    protected System.Web.UI.WebControls.TextBox TextBoxPoint;
    protected System.Web.UI.WebControls.TextBox TextBoxQuantity;
    protected System.Web.UI.WebControls.DropDownList DropDownListGrade;
    protected System.Web.UI.WebControls.DropDownList DropDownListDetail;
    protected System.Web.UI.WebControls.DropDownList DropDownListCategory;
    protected System.Web.UI.WebControls.TextBox TextBoxProductType;
    protected System.Web.UI.WebControls.TextBox TextBoxProductName;
    protected System.Web.UI.WebControls.Button ButtonClear;
    protected System.Web.UI.HtmlControls.HtmlInputFile FilePicUpLoad;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!this.IsPostBack)
    {
    InitCategory();
    }
    InitCategoryDetail(Int32.Parse(this.DropDownListCategory.SelectedValue));
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ButtonOK.Click += new System.EventHandler(this.ButtonOK_Click);
    this.ButtonClear.Click += new System.EventHandler(this.ButtonClear_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void ButtonOK_Click(object sender, System.EventArgs e)
    {
    string strPicName = "nopicture.gif";
    if(FilePicUpLoad.PostedFile!=null)
    {
    //修改成以上传时间为文件名
    strPicName = DateTime.Now.ToString("yyyyMMddhhMMss")+ Path.GetExtension(FilePicUpLoad.PostedFile.FileName);
    //修改路径
    string strPath = HttpContext.Current.Server.MapPath(".")+"\\Data\\products\\bigpic\\";
    strPath = strPath.ToLower().Replace("\\admin\\","\\");
    FilePicUpLoad.PostedFile.SaveAs(strPath+strPicName);
    strPath = HttpContext.Current.Server.MapPath(".")+"\\Data\\products\\smallpic\\";
    strPath = strPath.ToLower().Replace("\\admin\\","\\");
    FilePicUpLoad.PostedFile.SaveAs(strPath+strPicName);
    }
    int rst = ProductsDb.AddProduct(this.TextBoxProductName.Text,
    this.TextBoxProductType.Text,
    Int32.Parse(this.DropDownListGrade.SelectedValue),
    Int32.Parse(this.TextBoxMarketPrice.Text),
    Int32.Parse(this.TextBoxMemberPrice.Text),
    Int32.Parse(this.TextBoxVIPPrice.Text),
    Int32.Parse(this.TextBoxQuantity.Text),
    Int32.Parse(this.RadioButtonListSellState.SelectedValue),
    Int32.Parse(this.TextBoxPoint.Text),
    strPicName,
    this.TextBoxDescriptions.Text,
    Int32.Parse(this.DropDownListCategory.SelectedValue),
    Int32.Parse(this.DropDownListDetail.SelectedValue),
    Int32.Parse(this.RadioButtonListPick.SelectedValue));
    //保存成功,清空已输入的信息
    if(rst!=-1)
    {
    ClearTextBox();
    }
    }
    private void ClearTextBox()
    {
    TextBoxDescriptions.Text = "";
    TextBoxVIPPrice.Text = "";
    TextBoxMemberPrice.Text = "";
    TextBoxMarketPrice.Text = "";
    TextBoxPoint.Text = "";
    TextBoxQuantity.Text = "";
    TextBoxProductType.Text = "";
    TextBoxProductName.Text = "";
    }
    private void InitCategory()
    {
    DataSet ds = (DataSet)HttpContext.Current.Cache["Category"];
    this.DropDownListCategory.DataSource = ds.Tables["Categories"];
    this.DropDownListCategory.DataTextField = "CategoryName";
    this.DropDownListCategory.DataValueField = "CategoryId";
    this.DropDownListCategory.DataBind();
    } private void InitCategoryDetail(int categoryId)
    {
    DataSet ds = (DataSet)HttpContext.Current.Cache["Category"];
    ds.Tables["CategoryDetails"].DefaultView.RowFilter = "CategoryId = "+categoryId;
    this.DropDownListDetail.DataSource = ds.Tables["CategoryDetails"].DefaultView;
    this.DropDownListDetail.DataTextField = "DetailName";
    this.DropDownListDetail.DataValueField = "DetailId";
    this.DropDownListDetail.DataBind();
    } private void ButtonClear_Click(object sender, System.EventArgs e)
    {
    ClearTextBox();
    }
    }
    }
      

  4.   

    前台部分
    <%@ Page language="c#" Codebehind="ProductAdd.aspx.cs" AutoEventWireup="false" Inherits="CraftPortal.Admin.ProductAdd" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>ProductAdd</title>
    <link href="../ASPNETPortal.css" type="text/css" rel="stylesheet" >
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">

    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="600" align="center" border="1">
    <TR>
    <TD class="ProductEditTdHeight" width="100">商品名称</TD>
    <TD class="ProductEditTdHeight" width="200">
    <asp:TextBox id="TextBoxProductName" runat="server"></asp:TextBox></TD>
    <TD class="ProductEditTdHeight" width="100">商品型号</TD>
    <TD width="200">
    <asp:TextBox id="TextBoxProductType" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
    <TD>所属大类</TD>
    <TD>
    <asp:DropDownList id="DropDownListCategory" runat="server" AutoPostBack="True"></asp:DropDownList></TD>
    <TD>所属小类</TD>
    <TD>
    <asp:DropDownList id="DropDownListDetail" runat="server"></asp:DropDownList></TD>
    </TR>
    <TR>
    <TD>商品等级</TD>
    <TD>
    <asp:DropDownList id="DropDownListGrade" runat="server">
    <asp:ListItem Value="1">一星</asp:ListItem>
    <asp:ListItem Value="2">二星</asp:ListItem>
    <asp:ListItem Value="3">三星</asp:ListItem>
    <asp:ListItem Value="4">四星</asp:ListItem>
    <asp:ListItem Value="5" Selected="True">五星</asp:ListItem>
    </asp:DropDownList></TD>
    <TD>上货数量</TD>
    <TD>
    <asp:TextBox id="TextBoxQuantity" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
    <TD>赠送积分</TD>
    <TD>
    <asp:TextBox id="TextBoxPoint" runat="server"></asp:TextBox></TD>
    <TD>市场价格</TD>
    <TD>
    <asp:TextBox id="TextBoxMarketPrice" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
    <TD>会员价格</TD>
    <TD>
    <asp:TextBox id="TextBoxMemberPrice" runat="server"></asp:TextBox></TD>
    <TD>VIP价格</TD>
    <TD>
    <asp:TextBox id="TextBoxVIPPrice" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
    <TD>商品等级</TD>
    <TD>
    <asp:RadioButtonList id="RadioButtonListPick" runat="server" RepeatDirection="Horizontal" Width="152px">
    <asp:ListItem Value="1" Selected="True">普通商品</asp:ListItem>
    <asp:ListItem Value="0">经典商品</asp:ListItem>
    </asp:RadioButtonList></TD>
    <TD>是否销售</TD>
    <TD>
    <asp:RadioButtonList id="RadioButtonListSellState" runat="server" RepeatDirection="Horizontal" Width="152px">
    <asp:ListItem Value="1" Selected="True">是</asp:ListItem>
    <asp:ListItem Value="0">否</asp:ListItem>
    </asp:RadioButtonList></TD>
    </TR>
    <TR>
    <TD>商品图片</TD>
    <TD colSpan="3"><INPUT id="FilePicUpLoad" style="WIDTH: 370px; HEIGHT: 22px" type="file" size="42" name="FilePicUpLoad"
    runat="server"></TD>
    </TR>
    <TR>
    <TD>商品简介</TD>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD align="center" colSpan="4">
    <asp:TextBox id="TextBoxDescriptions" runat="server" Width="544px" Height="248px" TextMode="MultiLine"></asp:TextBox></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    <TD>
    <asp:Button id="ButtonClear" runat="server" Text="清除" CssClass="admininput"></asp:Button></TD>
    <TD>
    <asp:Button id="ButtonOK" runat="server" Width="48px" Text="添加"></asp:Button></TD>
    </TR>
    </TABLE>

    </form>
    </body>
    </HTML>
      

  5.   

    看看这个
    http://www.metabuilders.com/Tools/ListLink.aspx