在 DropdownList 中赋值时出现问题
我已经获得了项目键(保险类别)的值。但是,我似乎无法将其放回保险类别。我如何将其分配到保险类别?我需要这个才能保存/添加新的保险条目。
请举例说明如何将其分配到保险类别。
请检查下面的代码:
List<Value> lsValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
if (item.Key.ToString() == drpCategory.SelectedValue)
{
oInsurance.Category.value = item.Key;// error here
}
}
下拉列表代码:
private void InsuranceCategorySource() {
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
DataRow dr = dt.NewRow();
dr[0] = item.Key.ToString();
dr[1] = item.Value.ToString();
dt.Rows.Add(dr);
}
drpCategory.DataSource = ds;
drpCategory.DataMember = "Source";
drpCategory.DataTextField = "Desc";
drpCategory.DataValueField = "ID";
drpCategory.DataBind();
}
谢谢!
I already get the value of the item key (Insurance Category). However, I can't seem to put it back to Insurance Category. How will I assign it to Insurance Category? I need this in order for me to save/add a new Insurance Entry.
Please give me an example on how it will be assigned to the Insurance Category.
Please check my codes below:
List<Value> lsValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
if (item.Key.ToString() == drpCategory.SelectedValue)
{
oInsurance.Category.value = item.Key;// error here
}
}
Dropdownlist code:
private void InsuranceCategorySource()
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
DataRow dr = dt.NewRow();
dr[0] = item.Key.ToString();
dr[1] = item.Value.ToString();
dt.Rows.Add(dr);
}
drpCategory.DataSource = ds;
drpCategory.DataMember = "Source";
drpCategory.DataTextField = "Desc";
drpCategory.DataValueField = "ID";
drpCategory.DataBind();
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议将 Dropdown 的
DisplayMember
设置为 Value,将 Dropdown 的ValueMember
设置为 Key。如果您正确设置列表的
DisplayMember
和ValueMember
,您的代码就可以工作。由于您使用 ASP.NET 属性来正确设置将是 DataTextField 和
It would be recommended if you set the
DisplayMember
of the Dropdown to the Value and theValueMember
of the Dropdown to the Key.If you set the
DisplayMember
and theValueMember
of the list correctly your code would work.Since you're using ASP.NET properties to set correctly would be DataTextField and DataValueField in ASP.NET
如果我弄错了,请纠正我。您首先检查它们与下拉列表中所选值的键,然后根据您尝试将其“值”输入到保险类别?
Please correct me if i am getting it wrong. You are first checking for they key with the selected value of Drop down list and then based on that you are trying to enter its "value" to insurance category?