为什么DataValueField返回的总是第一个值
private Blog _bl;
protected void Page_Load(object sender, EventArgs e)
{
ClassID.DataSource = Business.GetAllClass().Tables[0].DefaultView;
ClassID.DataTextField = "ClassName";
ClassID.DataValueField = "ClassID";
ClassID.DataBind();
}
protected void AddRow(object sender, EventArgs e)
{
_bl = new Blog();
_bl.ClassID = ClassID.SelectedValue;
_bl.BlogTitle = BlogTitle.Value;
_bl.BlogContent = BlogContent.Value;
Business.AddBlog(_bl);
Response.Redirect("default.aspx");
}
表的情况如下
classid classname
1 class1
2 class2
3 class3
4 class4
返回的值总是1 ,我要的效果是比如选 class3 返回 3
------解决方案--------------------放到ispostback里面
------解决方案--------------------protected void Page_Load(object sender, EventArgs e)
{ if (IsPostback == false) {
ClassID.DataSource = Business.GetAllClass().Tables[0].DefaultView;
ClassID.DataTextField = "ClassName";
ClassID.DataValueField = "ClassID";
ClassID.DataBind();
}
}
------解决方案-------------------- protected void Page_Load(object sender, EventArgs e)
{
if(!Page.Ispostback)
{
ClassID.DataSource = Business.GetAllClass().Tables[0].DefaultView;
ClassID.DataTextField = "ClassName";
ClassID.DataValueField = "ClassID";
ClassID.DataBind();
}
}
加上```就行了``
因为你每次页面加载都会绑定``你获取的值只有是1喽``