----------aab.aspx--------------
<SCRIPT LANGUAGE="JavaScript">
<!--
function XmlPost(obj,fullName)
{
var svalue = obj.value;
var fullObj = document.all(fullName);
var webFileUrl = "?sortid=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "" && svalue != "")
{
fullObj.length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
var ary1 = piArray[i].toString().split("|");
fullObj.options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
{
fullObj.length = 0;
fullObj.options.add(new Option("请选择",""));
}
}
//-->
</SCRIPT>
<asp:ListBox id="anclassid" runat="server" Width="144px" Height="40px" Rows="1"></asp:ListBox>
<asp:ListBox id="nclassid" runat="server" Width="144px" Height="40px" Rows="1"></asp:ListBox>
--------------------aab.cs---------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
string sortid = this.Request.QueryString["sortid"];
if(sortid + "a" != "a")
{
this.xmlBind(sortid);
}
if(!this.IsPostBack)
{
this.DownBind1();
this.DownBind2();
}
}
private DataTable Get_Dt(string sql)
{
net.BasalClass.Conn.Conn obj = new net.BasalClass.Conn.Conn();
obj.open();
SqlDataAdapter myAdp = new SqlDataAdapter(sql,obj.myconn);
DataTable myDt = new DataTable();
try
{
myAdp.Fill(myDt);
return(myDt);
}
catch(SqlException ex)
{
throw ex;
}
finally
{
obj.myconn.Close();
}
}
private void xmlBind(string sortid)
{
string mystr = "";
string sql = "select nclass,nclassid,anclassid from nclass where anclassid = " + sortid ;
DataTable mytab = this.Get_Dt(sql);
if(mytab.Rows.Count != 0)
{
for(int i=0;i<mytab.Rows.Count;i++)
{
mystr += "," + mytab.Rows[i]["nclassid"].ToString() + "|" + mytab.Rows[i]["nclass"].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
private void DownBind1()
{
string sql = "select anclassid,anclass from anclass order by anclassorder";
DataTable mytab = this.Get_Dt(sql);
this.anclassid.DataSource = mytab;
this.anclassid.DataValueField = "anclassid";
this.anclassid.DataTextField = "anclass";
this.anclassid.DataBind();
this.anclassid.Items.Insert(0,new ListItem("请选择分类",""));
ListItem myItem = this.anclassid.Items.FindByValue("1");
if(myItem != null)
{
myItem.Selected = true;
}
this.anclassid.Attributes.Add("onchange","XmlPost(this,'" + this.nclassid.ClientID + "');");
}
private void DownBind2()
{
string sql = "select nclassid,nclass,anclassid from nclass where anclassid =1" ;
DataTable mytab = this.Get_Dt(sql);
this.nclassid.DataSource = mytab;
this.nclassid.DataSource = mytab;
this.nclassid.DataValueField = "nclassid";
this.nclassid.DataTextField = "nclass";
this.nclassid.DataBind();
this.nclassid.Items.Insert(0,new ListItem("请选择名称",""));
}
private void addusers_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
BasalClass.Conn.Conn obj = new net.BasalClass.Conn.Conn();
string password = FormsAuthentication.HashPasswordForStoringInConfigFile(userpass.Text,"md5");
string sql="InSert InTO [webuser] (anclassid,nclassid) values ("+anclassid.SelectedItem.Value.ToString()+","+nclassid.SelectedItem.Value.ToString()+")";
obj.open();
SqlCommand mycommand=new SqlCommand(sql,obj.myconn);
try
{
mycommand.ExecuteNonQuery();
mycommand.Dispose();
obj.myconn.Close();
obj.myconn.Dispose();
Response.Write("<script>alert('添加成功!');location='admin_adduser.aspx'</script>");
}
catch(System.Data.SqlClient.SqlException)
{
mycommand.Dispose();
obj.myconn.Close();
obj.myconn.Dispose();
Response.Write("<script>alert('对不起,操作出现错误,请返回!!');location='admin_adduser.aspx'</script>");
}
}
}
为什么nclassid选择框可以选择内容,但保存的时候提示“未将对象引用设置到对象的实例。”
好像是nclassid没有值。
[此贴子已经被作者于2006-1-7 13:29:23编辑过]