页面初始化时的代码为
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlUserName.DataSource = UserList;
this.ddlUserName.DataTextField = "Name";
this.ddlUserName.DataValueField = "Id";
this.ddlCustomerName.DataBind();
}
}
protected void ddlUserName_SelectedIndexChanged(object sender, EventArgs e)
{
int userId = TypeParse.IntParse(this.ddlUserName.SelectedValue);
}
以上这段代码可以正确获得userId。调试查看“this.ddlUserName.SelectedValue”即为''this.ddlUserName.SelectedValue''中的值,即数据库中''ID”对应的值。
但是我把程序改为
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlUserName.DataSource = UserList;
this.ddlUserName.DataTextField = "Name";
//this.ddlUserName.DataValueField = "Id";
this.ddlCustomerName.DataBind();
}
}
protected void ddlUserName_SelectedIndexChanged(object sender, EventArgs e)
{
int userId = TypeParse.IntParse(this.ddlUserName.SelectedValue);
}
即将this.ddlUserName.DataValueField = "Id";注释掉,本以为通过查看“this.ddlUserName.SelectedValue”获得的应该为空字符串,但是结果获得的却是“this.ddlUserName.DataTextField ”中的值,即“Name”。
这是我不明白的地方,希望高人指点。
一个属性是DataTextField,一个是DataValueField
郎朗坤
波斯汪
MMTTMM
千巷猫影
温温酱