| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 669 人关注过本帖
标题:级联选择框的问题!
只看楼主 加入收藏
zhuangjunx
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-1-2
收藏
 问题点数:0 回复次数:0 
级联选择框的问题!

----------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编辑过]

搜索更多相关主题的帖子: 级联 选择 
2006-01-07 13:28
快速回复:级联选择框的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061894 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved