I am trying to set the value of a dropdownlist using an integer id value from a table. But whatever syntax I use (SelectedValue, SelectedItem,开发者_如何学运维 SelectedIndex), I keep getting a cannot convert int to string error. Here is an example of my code :
ddlSupContracts2.SelectedItem.Value = Convert.ToInt32(ObjMeter.intSupplierContract);
ObjMeter.intSupplierContract will be an integer. How do I use this value to set the selected item in my dropdownlist?
Try
ddlSupContracts2.Items.FindByValue(ObjMeter.intSupplierContract.ToString()).Selected = true;
or
ddlSupContracts2.SelectedValue = ObjMeter.intSupplierContract.ToString();
You need to set the value as a String instead of an integer as SelectedItem.Value
property is String type.
ddlSupContracts2.SelectedItem.Value = ObjMeter.intSupplierContract.ToString();
OR it can be
ddlSupContracts2.SelectedValue = ObjMeter.intSupplierContract.ToString();
精彩评论