我将value绑定到数据库的ID字段了,text绑定到了名称字段,但是在取ID的时候却只有1,就是不管你选什么都是1
我绑定的代码如下
private void AddUserDepNameBind()
{
BLL.Accounts_Department bll = new BLL.Accounts_Department();
DataSet ds = bll.GetAllList();
this.AddUserDepName.DataSource = ds.Tables[0];
this.AddUserDepName.DataTextField = "DepName";
this.AddUserDepName.DataValueField = "DepartmentID";
this.AddUserDepName.DataBind();
}
我取值的代码是这样的额
protected void SaveInfomation_Click(object sender, EventArgs e)
{
string strError = "";
if (this.AddUserName.Text.Trim().Length==0)
{
strError += "用户名不能为空!\\n";
}
if (this.AddUserPwd.Text.Trim().Length==0)
{
strError += "密码不能为空\\n";
}
if (this.AddTrueName.Text.Trim().Length==0)
{
strError += "真实姓名不能为空\\n";
}
if (!PageValidate.IsNumber(AddUserAge.Text))
{
strError += "年龄格式错误\\n";
}
if (!PageValidate.IsDateTime(AddUserbirthday.Text))
{
strError += "出生日期格式错误\\n";
}
if (this.AddUserPhone.Text.Trim().Length==0)
{
strError += "电话号码不能为空\\n";
}
if (this.AddUserEmail.Text.Trim().Length==0)
{
strError += "E-Mail不能为空\\n";
}
if (strError!="")
{
MessageBox.Show(this.Page,strError);
return;
}
string UserName = this.AddUserName.Text;
byte[] UserPassword = EncryptPassword(this.AddUserPwd.Text);
string TrueName = this.AddTrueName.Text;
string Sex = this.AddUserSex.SelectedValue.ToString();
int Age = int.Parse(this.AddUserAge.Text);
DateTime BirthDay = DateTime.Parse(this.AddUserbirthday.Text);
string Phone = this.AddUserPhone.Text;
string Email = this.AddUserEmail.Text;
int DepName =Convert.ToInt32( this.AddUserDepName.SelectedValue);
string UserType = this.AddUserType.SelectedValue.ToString();
Model.Accounts_Users model = new Model.Accounts_Users();
model.UserName = UserName;
model.UserPassWord = UserPassword;
model.TrueName = TrueName;
model.Sex = Sex;
model.Age = Age;
model.BirthDay = BirthDay;
model.Phone = Phone;
model.Email = Email;
model.DepartmentID = DepName;
model.UserType = UserType;
BLL.Accounts_Users bll = new BLL.Accounts_Users();
bll.Add(model);
Common.MessageBox.ShowAndRedirect(this.Page, "保存成功!", "User.aspx");
}
page_Load是这样的额
protected void Page_Load(object sender, EventArgs e)
{
AddUserDepNameBind();
AddUserTypeBind();
if (!IsPostBack)
{
//AddUserDepNameBind();
//AddUserTypeBind();
}
}
AddUserDepNameBind()方法放在if(!ispostback){ }里面
string Sex = this.AddUserSex.SelectedValue.ToString();
你的 sex 取值正确吗?
给Page 头 加上 EnableViewState="true" 试试
是保存的时候报错么? ... 还是加断点调试吧 。一步一步跟下来。 看到哪行代码报错。如果是bll.Add(model) 方法报错的话就近方法里看看具体哪行。
取ID的时候却只有1 ??? 需要把AutoPostBack设置为true