简体   繁体   English

虽然DataTextField已检索,但DataValueField未检索值

[英]DataValueField didn't retrieve value ,although DataTextField retrieved

while I'm trying to use the DataValueField of my dropdownlist as an input int parameter in a method by using parse method int.parse(DataValueField) as its value should come from the database it gives me this runtime error 虽然我试图通过使用parse方法int.parse(DataValueField)将我的dropdownlist的DataValueField用作方法中的输入int参数,因为它的值应该来自数据库,它给了我这个运行时错误

Input string was not in a correct format 输入字符串的格式不正确

so I debugged the website to see where is the problem I found that it didn't retrieved its value int its SelectedIndexChanged method ,although the dropdownlist was filled in the Page_Load method 所以我调试了网站,看看问题出在哪里,我发现它没有在SelectedIndexChanged方法中检索到它的值,尽管在Page_Load方法中填充了下拉列表
here is the page_load method : 这是page_load方法

protected void Page_Load(object sender, EventArgs e)
{
writerddl.DataSource = DS.show_all_writers();
writerddl.DataValueField = "writerid";
writerddl.DataTextField = "writersname:;
writerddl.DataBind(); }  

and this is the SelectedIndexChanged method: 这是SelectedIndexChanged方法:

protected void writerddl_SelectedIndexChanged(object sender, EventArgs e)
{
writer _writer = DS.select_writer_by_id(int.Parse(writerddl.DataValueField)).Single();
usernametxt.Text = _writer.username;
passwordtxt.Text = _writer.password;
nametxt.Text = _writer.writersname;
}

Note that I'm using LinQtoSql ,so DS."method"() is a stored procedure 请注意,我使用的是LinQtoSql ,所以DS。“method”()是一个存储过程

Replace this: 替换这个:

writer _writer = DS.select_writer_by_id(int.Parse(writerddl.DataValueField)).Single();

with: 有:

int i = int.Parse(writerddl.SelectedItem.Value);
writer _writer = DS.select_writer_by_id(i).Single();

In msdn DataValueField 在msdn DataValueField中

Use this property to specify the field that contains the value of each item in a list control. 使用此属性指定包含列表控件中每个项的值的字段。

So its simply property name, not value. 所以它的简单属性名称,而不是价值。 For getting selected value you need use 要获得选定的价值,您需要使用

  1. SelectedValue 的SelectedValue
  2. SelectedItem 的SelectedItem

To select the selected value from dropdown list, you should use writerddl.SelectedValue instead off writerddl.DataValueField 要从下拉列表中选择所选值,您应该使用writerddl.SelectedValue而不是writerddl.DataValueField

Thanks Parvati 谢谢Parvati

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM