Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column named productid.It has length equal 10.I have set maxlength of the column equal 10.I load DataTable contains this column into DropDownList.I can not select the item using:
C#
ddl.selectedValue=tbl.Rows[0][0].tostring();
//tbl.Rows[0][0].tostring()='PS001'

after checking the length, I get the ddl's length:10 and tbl.Rows[0][0].tostring()'s lenght:5.SelectValue was attached to the whitespace.
I can't
C#
ddl.selectedValue.trim()=tbl.Rows[0][0].tostring();

I tried not set MaxLength.I still get ddl's length equal 10
How to fix it?
Posted

i didn't understand entirely what you mean but perhaps you are using trim() on the wrong side!
use this instead:
C#
ddl.selectedValue=tbl.Rows[0][0].tostring().Trim();
 
Share this answer
 
v2
When you load your ddl, you have to provide a known value, which will become the selectedvalue.

When you set the value, the value you reference has to match one of the known values, or else you can't set the ddl value.
ddl.items.add(new listitem("red", "red"))
ddl.selectedvalue = "red"

I know it doesn't relate to your example, but I'm trying to convey the principal of selectedvalue

When in doubt, check the browser browser source (view source), and find your ddl, and look at the tags, for the format of your values
<option value="red">red</option>
 
Share this answer
 
Comments
giatuan2011 18-Jun-12 14:05pm    
Sorry,I use DropdownList in GridView.My RowDataBound:
protected void grdContact_RowDataBound(object sender, GridViewRowEventArgs e)
{
ContactTypeTableAdapter contactType = new ContactTypeTableAdapter();
DataTable contactTypes = GetData();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbType");
if (cmbType != null)
{
cmbType.DataSource = contactTypes;
cmbType.DataTextField = "ProductName";
cmbType.DataValueField = "ProductId";
cmbType.DataBind();
cmbType.SelectedValue = grdContact.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
//DataKeysName is ProductName.
}
I can get selectedValue when using:
grdContact.DataKeys[e.Row.RowIndex].Values[0].ToString().Insert(5," ");
if Productid is 'PS00000001',It can't get PS001

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900