我在做一个购物网站的系统,有商品的大类和小类这两个类别,小类类别里有个dropdownlist,包含了所有大类的名称信息,可是我在加入数据库时,却总是加的第一个大类类别的ID,其他ID加不了,dropdownlist的绑定是在if(!Page.IsPostBack){}里绑定的,然后根据dropdownlist的所选项得到ID,加到数据库里的,但仍然是第一个大类类别的ID,这是为什么呢?我很郁闷。
而且,我用DataGrid绑定数据库的信息时,DataGrid中也有一个dropdownlist,内容也是大类类别的ID,加到数据库的还是第一个大类ID,我快崩溃了,帮帮我吧!
对了,DataGrid模板中的Dropdownlist我并没有在if(!Page.IsPostBack){}中绑定,我是写了一个返回DataSet的方法,在DropDownList属性的DataBing中的自定义中写入了这个方法名称的,这样做对吗?
在得到大类类别ID时我分别用了两种方法,都不行,第一种是:根据DropDownList所选的项,是大类类别名称,作为参数写了个方法,返回大类类别的ID
第二种是:用ViewState["id"]=this.DropDownList1.SelectedValue.ToString();
两种方法都不行,为什么呢?
期待你们...先谢谢了!
而且,我用DataGrid绑定数据库的信息时,DataGrid中也有一个dropdownlist,内容也是大类类别的ID,加到数据库的还是第一个大类ID,我快崩溃了,帮帮我吧!
对了,DataGrid模板中的Dropdownlist我并没有在if(!Page.IsPostBack){}中绑定,我是写了一个返回DataSet的方法,在DropDownList属性的DataBing中的自定义中写入了这个方法名称的,这样做对吗?
在得到大类类别ID时我分别用了两种方法,都不行,第一种是:根据DropDownList所选的项,是大类类别名称,作为参数写了个方法,返回大类类别的ID
第二种是:用ViewState["id"]=this.DropDownList1.SelectedValue.ToString();
两种方法都不行,为什么呢?
期待你们...先谢谢了!
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;namespace Logo
{
/// <summary>
/// shangpingLb 的摘要说明。
/// </summary>
public class shangpingLb : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.HtmlControls.HtmlTextArea TEXTAREA1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.HtmlControls.HtmlTextArea TEXTAREA2;
protected System.Web.UI.WebControls.TextBox TextBox2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
this.DropDownList1.DataSource=bigManage.selectBigSortname();
this.DropDownList1.DataBind();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
ClassLibrary.BigSortBean bigBean=new ClassLibrary.BigSortBean();
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
ClassLibrary.PublicManage pm=new ClassLibrary.PublicManage();
string s=bigManage.CheckName(this.TextBox1.Text);
if(s=="")
{
bool b=false;
bigBean.BigName=this.TextBox1.Text.Trim();
bigBean.Memo=this.TEXTAREA1.Value.Trim();
try
{
b=bigManage.InsertBigSort(bigBean);
} catch(Exception ec)
{
ClassLibrary.PublicManage pmp=new ClassLibrary.PublicManage();
pmp.ErrorTransact(ec);
}
if(b)
{
this.Label3.Text="添加成功!";
} else
{
this.Label3.Text="添加失败!";
}
}
else
{
this.Label1.Text=s.ToString();
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
ClassLibrary.SmallManage smallManage=new ClassLibrary.SmallManage();
ClassLibrary.SmallSortBean smaBean=new ClassLibrary.SmallSortBean();
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
string s=smallManage.CheckName(this.TextBox2.Text);
SortedList li=smallManage.sName();
if(s=="")
{
bool b=false;
smaBean.SmallName=this.TextBox2.Text;
smaBean.BigID=bigManage.SelectBigSort(this.DropDownList1.SelectedValue.ToString()).BigID;//这是第一种方法
smaBean.Memo=this.TEXTAREA2.Value.Trim(); try
{
b=smallManage.insertSmall(smaBean);
} catch(Exception ex)
{
throw ex;
} if(b)
{
this.Label4.Text="添加成功!";
} else
{
this.Label4.Text="添加失败!";
}
} else
{
this.Label2.Text=s.ToString();
} }
}
}
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;namespace Logo
{
/// <summary>
/// shangpingLb 的摘要说明。
/// </summary>
public class shangpingLb : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.HtmlControls.HtmlTextArea TEXTAREA1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.HtmlControls.HtmlTextArea TEXTAREA2;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
//this.DropDownList1.DataSource=(bigManage.SelectsBigSort()).Tables["BigSortInfo"].Columns["BigSortName"];
this.DropDownList1.DataSource=bigManage.selectBigSortname();
/*SortedList list=bigManage.select();
for(int i=0;i<list.Count;i++)
{
this.DropDownList1.Items.Add(list.GetByIndex(i).ToString());
}*/
this.DropDownList1.DataBind();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
ClassLibrary.BigSortBean bigBean=new ClassLibrary.BigSortBean();
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
ClassLibrary.PublicManage pm=new ClassLibrary.PublicManage();
string s=bigManage.CheckName(this.TextBox1.Text);
if(s=="")
{
bool b=false;
bigBean.BigName=this.TextBox1.Text.Trim();
bigBean.Memo=this.TEXTAREA1.Value.Trim();
try
{
b=bigManage.InsertBigSort(bigBean);
} catch(Exception ec)
{
ClassLibrary.PublicManage pmp=new ClassLibrary.PublicManage();
pmp.ErrorTransact(ec);
Response.Write(ec.ToString());
}
if(b)
{
this.Label3.Text="添加成功!";
} else
{
this.Label3.Text="添加失败!";
}
}
else
{
this.Label1.Text=s.ToString();
}
}///////////////////////////////////////////////////////就是这里
private void Button2_Click(object sender, System.EventArgs e)
{
this.Label5.Text=this.DropDownList1.SelectedIndex.ToString();
ClassLibrary.SmallManage smallManage=new ClassLibrary.SmallManage();
ClassLibrary.SmallSortBean smaBean=new ClassLibrary.SmallSortBean();
ClassLibrary.BigSortManage bigManage=new ClassLibrary.BigSortManage();
string s=smallManage.CheckName(this.TextBox2.Text);
SortedList li=smallManage.sName();
//ViewState["id"]=this.DropDownList1.SelectedItem.Text; if(s=="")
{
bool b=false;
smaBean.SmallName=this.TextBox2.Text;
smaBean.BigID=bigManage.SelectBigSort(this.DropDownList1.SelectedItem.Text).BigID;//方法1
//smaBean.BigID=Convert.ToInt32(ViewState["id"]);//方法2
smaBean.Memo=this.TEXTAREA2.Value.Trim(); try
{
b=smallManage.insertSmall(smaBean);
} catch(Exception ex)
{
throw ex;
} if(b)
{
this.Label4.Text="添加成功!";
} else
{
this.Label4.Text="添加失败!";
}
} else
{
this.Label2.Text=s.ToString();
} } private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//ViewState["id"]=this.DropDownList1.SelectedItem.Text;
}
}
}
那我上面代码里面用得对吗?
<asp:DropDownList ID="ddl" runat="server" EnableViewState="false"></asp:DropDownList>