我将value绑定到数据库的ID字段了,text绑定到了名称字段,但是在取ID的时候却只有1,就是不管你选什么都是1
我绑定的代码如下
- C# code
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(); }
我取值的代码是这样的额
- C# code
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"; } /*string EmailRegex=@"^[/w-]+(/.[/w-]+*@[/w-]+(/.[/w=]+)+$"; if (Regex.IsMatch(AddUserEmail.Text,EmailRegex)) { Response.Write("<script>alt"+"邮箱格式错误"+"</script>"); }*/ if (strError!="") { MessageBox.Show(this.Page,strError); return; } // BLL.Accounts_Department Depbll = new BLL.Accounts_Department(); //定义以及初始化 string UserName = this.AddUserName.Text; //byte[] UserPassword = new UnicodeEncoding().GetBytes(this.AddUserPwd.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 = this.AddUserDepName.SelectedIndex; //int DepName = Convert.ToInt32(this.AddUserDepName.Items[0].Value); //int DepName =Convert.ToInt32( Depbll.GetID(this.AddUserDepName.Text)); //int DepName =Convert.ToInt32( this.AddUserDepName.DataValueField); //int DepName =Convert.ToInt32( this.AddUserDepName.SelectedItem.Value); int DepName =Convert.ToInt32( this.AddUserDepName.SelectedValue); //int DepName = AddUserDepName_SelectedIndexChanged(DepName); 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是这样的额
- C# code
protected void Page_Load(object sender, EventArgs e) { AddUserDepNameBind(); AddUserTypeBind(); if (!IsPostBack) { //AddUserDepNameBind(); //AddUserTypeBind(); } }