Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this below code which I am using to change the selected value of dropdownlist. This dropdownlist items are not data-bound and I have added items to them at design time.

C#
if (dataTable.Rows.Count > 0 && dataTable.Columns.Count > 0)
{
  if (dataTable.Rows[0]["IsSpeechActive"].ToString().Equals(bool.TrueString)) { chkSpeechActive.Checked = true; } else { chkSpeechActive.Checked = false; }
  ddlSL1.SelectedValue = dataTable.Rows[0]["Value1"].ToString(); //en-us
  ddlSL2.SelectedValue = dataTable.Rows[0]["Value2"].ToString(); //1.0
  ddlSL3.SelectedValue = dataTable.Rows[0]["Value3"].ToString(); //1.5
  ddlSL4.SelectedValue = dataTable.Rows[0]["Value4"].ToString(); //default
  ddlSL5.SelectedValue = dataTable.Rows[0]["Value5"].ToString(); //0.5
}
else
{
  ShowMessage("Could not find user speech details");
}


ASP.NET
<asp:DropDownList ID="ddlSL5" CssClass="cm-input-dataform-select cm-input-font" runat="server">
  <asp:ListItem Text="0.0" Value="0.0"></asp:ListItem>
  <asp:ListItem Text="0.1" Value="0.1"></asp:ListItem>
  <asp:ListItem Text="0.2" Value="0.2"></asp:ListItem>
  <asp:ListItem Text="0.3" Value="0.3"></asp:ListItem>
  <asp:ListItem Text="0.4" Value="0.4"></asp:ListItem>
  <asp:ListItem Text="0.5" Value="0.5"></asp:ListItem>
  <asp:ListItem Text="0.6" Value="0.6"></asp:ListItem>
  <asp:ListItem Text="0.7" Value="0.7"></asp:ListItem>
  <asp:ListItem Text="0.8" Value="0.8"></asp:ListItem>
  <asp:ListItem Text="0.9" Value="0.9"></asp:ListItem>
  <asp:ListItem Text="1.0" Value="1.0"></asp:ListItem>
</asp:DropDownList>


In the code above the first dropdown works fine but others are not changing the value to the one that I am fetching from the DB and setting them to.

Please advise as to what is wrong? A weird error this is.

What I have tried:

I deleted the asp.net web form and re-created it.
Posted
Updated 17-Dec-18 2:54am

First off, if you are pre-selecting values at SelectedIndexChanged event of DropDownList, then you need to set AutoPostback to TRUE to trigger the SelectedIndexChanged event.

Second, if you are binding and doing the pre-selection at Page_Load event, then make sure you wrap your code within !IsPostback block.

Third, try to do something like this to pre-select value based from the data in your database:

C#
ListItem item = ddlSL5.Items.FindByValue(dataTable.Rows[0]["Value5"].ToString());
if (item != null){
     	ddlSL5.ClearSelection();
     	item.Selected = true;
}


Do the same for the rest of your DropDownLists.
 
Share this answer
 
v2
Comments
Christopher Fernandes 17-Dec-18 0:32am    
All listitems in all dropdownlist are added at design time. There is no data binding & index change event that I am using in this form. Only trying to select values in all dropdownlist with what is fetched from the database table at page load & update button click event.
Vincent Maverick Durano 17-Dec-18 1:04am    
The code provided doesn't matter if you were added items on design time. Have you tried it?
Christopher Fernandes 17-Dec-18 1:29am    
I am trying this code now

string value = dataTable.Rows[0]["Value5"].ToString();
if (ddlSL5.Items.FindByValue(value) != null) { ddlSL5.Items.FindByValue(value).Selected = true; }

Even this is not working. Is this because the list items are hard coded and not databound to the dropdownlist.
Christopher Fernandes 17-Dec-18 1:53am    
the above code does not work
Vincent Maverick Durano 17-Dec-18 7:57am    
What is the value of the value variable? does the value of your value variable exists in one of your DropDownList?
This code below solved the issue

C#
string value = ""; decimal rowvalue;
rowvalue = decimal.Parse(dataTable.Rows[0]["Value5"].ToString());
value = Convert.ToString(rowvalue.ToString("0.0"));
if (ddlSL5.Items.FindByValue(value) != null) { ddlSL5.Items.FindByValue(value).Selected = true; }
 
Share this answer
 

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