c#中dropdownlist取value值
我将value绑定到数据库的ID字段了,text绑定到了名称字段,但是在取ID的时候却只有1,就是不管你选什么都是1我绑定的代码如下privatevoidAddUserD...
我将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();
}
}
用ajax后绑定数据问题 展开
我绑定的代码如下
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();
}
}
用ajax后绑定数据问题 展开
5个回答
展开全部
我将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();
}
}
我绑定的代码如下
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){ }里面
更多追问追答
追问
不好意思啊 我这个是原来的 ,我放到if(!IsPostBack)里面了还是报错,说是输入的字符串格式不正确
追答
输入字符串格式不正确 就是里在往数据库里加数据的时候的问题了 注意字段类型 加个断点调试一下 看具体错在哪里
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string Sex = this.AddUserSex.SelectedValue.ToString();
你的 sex 取值正确吗?
给Page 头 加上 EnableViewState="true" 试试
你的 sex 取值正确吗?
给Page 头 加上 EnableViewState="true" 试试
更多追问追答
追问
正确的啊
追答
给Page 头 加上 EnableViewState="true" 试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是保存的时候报错么? ... 还是加断点调试吧 。一步一步跟下来。 看到哪行代码报错。如果是bll.Add(model) 方法报错的话就近方法里看看具体哪行。
更多追问追答
追问
三层都没有问题,就是取值出问题,也就是说我数据库没有0这个数据,存进去的时候是有外键约束的,所以会造成冲突
追答
1、确定AddUserDepName 绑定是正确的?
2、确定
if (!IsPostBack)
{
AddUserDepNameBind();
AddUserTypeBind();
}
确定AddUserDepNameBInd()是在 !IsPostBack 内
3、确定你已经重新编译通过了.
4、还是不行你可以 AddUserDepName.Item.SelectValue 这么试试。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
取ID的时候却只有1 ??? 需要把AutoPostBack设置为true
更多追问追答
追问
如果设置为true那么我在选择的时候页面会刷新
追答
你使用ajax特效的UpdatePanel1控件啊~~·
不设置autopostback为true,你永远只能取到第一个值~~
……………………
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询