This is a migrated thread and some comments may be shown as answers.

Wrong index selected.

6 Answers 63 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 1
Pierre asked on 24 Jun 2016, 12:33 PM
01. protected void RDDL_Statut_Load(object sender, EventArgs e)
02.{
03.    DC_FiltragesDataContext Mydb = new DC_FiltragesDataContext();
04.    var results = (from U in Mydb.PLOP
05.                   select new { statut = U.STATUT}).Distinct();
06. 
07.    RDDL_Statut.DataSource = results;
08.    RDDL_Statut.DataTextField = "statut";
09.    RDDL_Statut.DataValueField = "statut";
10. 
11.    RDDL_Statut.DataBind();
12.    RDDL_Statut.Items.Insert(0, new DropDownListItem("--ALL STATUT--", "ALL"));
13. 
14.}

            Hi, I have an issue with my drop down list.

Lets say that i have a DropDownList Displaying : A, B ,C , D etc. And I need a default item.
Using this code , my data is not correctly bound.
The display of the DrowDownList Is the one i expected . But my "RDDL_Statut.SelectedItem.Value.ToString()" Is returning the Next Index.
When A is clicked I get B value.


6 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 29 Jun 2016, 08:47 AM
Hello Pierre,

Which event handler do you access the SelectedItem.Value property in? If you want to get the selected item's value I would suggest subscribing to the SelectedIndexChanged event and in its handler you can use the arguments to get it: e.Value.

Regards,
Ivan Danchev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Pierre
Top achievements
Rank 1
answered on 30 Jun 2016, 07:01 AM

Hi,

I'm using the SelectedIndexChanged with my

protected void RDDL_Phase_ItemSelected(object sender, DropDownListEventArgs e)
{ RG_ProduitEC.Rebind(); }

to rebind my RadGrid.
On Selecting of my LinqDataSource I Check All my Dropdownlist With

if (RDDL_Statut.SelectedItem !=null && RDDL_Statut.SelectedItem.Value.ToString() != "ALL")
  results=results.Where(s => s.XXX.Equals(RDDL_Statut.SelectedItem.Value.ToString() )) ;
}

0
Ivan Danchev
Telerik team
answered on 04 Jul 2016, 03:14 PM
Hello Pierre,

The provided information is not enough for us to be able to determine what causes the behavior you have experienced. I would suggest isolating the DropDownList in a sample runnable project, which reproduces the issue, open a support ticket and attach it for further review. Here are our guidelines, which describe the correct approach for isolating an issue.

Regards,
Ivan Danchev
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Pierre
Top achievements
Rank 1
answered on 08 Jul 2016, 08:41 AM

Hi,
Let me help you reproduce this behavior.
First in the aspx :

<telerik:RadDropDownList ID="RDDL_Phase" runat="server" Skin="Vista" DataTextField="text" DataValueField="value" OnLoad="RDDL_Phase_Load" OnSelectedIndexChanged="RDDL_Phase_SelectedIndexChanged"  AutoPostBack="true" >
</telerik:RadDropDownList>
<h2><telerik:RadLabel runat="server" ID="Yeah" Text="HERE IS MA VALUE"></telerik:RadLabel></h2>

 

Then in the code behind:

01.public DataTable Get_RDDL_lorenipsum()
02.{
03.    DataTable data = new DataTable();
04.    data.Columns.Add("ID"); data.Columns.Add("Name");
05. 
06.    List<string> Value = new List<string>() { "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ" };
07.    foreach (var item in Value)
08.    {
09.        data.Rows.Add(item, item);
10.    }
11.    return data;
12.}
13. 
14.protected void RDDL_Phase_Load(object sender, EventArgs e)
15.{
16.    RDDL_Phase.DataSource = this.Get_RDDL_lorenipsum();
17.    RDDL_Phase.DataTextField = "Name";
18.    RDDL_Phase.DataValueField = "ID";
19.    RDDL_Phase.DataBind();
20.    RDDL_Phase.Items.Insert(0, new DropDownListItem("--GOO GOO GOO--", "ALL"));
21.}
22.protected void RDDL_Phase_SelectedIndexChanged(object sender, DropDownListEventArgs e)
23.{
24.    this.Yeah.Text = e.Value;
25.}

Voila !

Btw, i never had so mutch issue posting on a forum .. The backspace refresh and the blockcode Eternal trap re fun to see.

0
Pierre
Top achievements
Rank 1
answered on 08 Jul 2016, 09:05 AM
The only way I see to fix this is to had a row to a specific index in a datatable.
But the ordering of a datatable is based on the sorting..
So the only way to workaround is to a dummy column in the datatable n filter on it.

this is a lot of work around when Dropdown list . Items .insert should have done it.

If there is any other solution please let me know. For now the Drop Down list have been removed.

0
Accepted
Ivan Danchev
Telerik team
answered on 08 Jul 2016, 12:32 PM
Hello Pierre,

Thank you for posting a sample runnable page.

After analyzing the code we can say that the issue is caused by rebinding the DropDownList on every postback in the RDDL_Phase_Load handler. What happens is the following:
  • after a selection is made, the control initiates a postback sending the info about the SelectedIndex to the server, so for example if you select CC the SelectedIndex is 3.
  • on the server the  RDDL_Phase_Load handler is reached and you rebind the control which means after you call RDDL_Phase.DataBind(); the control contains one item less (the previously added --GOO GOO GOO-- item at this point does not exist in the items collection) so now the index 3 points at a different item (DD).
If you need to rebind the DropDownList on every postback, i.e. if binding it only on initial postback:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RDDL_Phase.DataSource = Get_RDDL_lorenipsum();
        RDDL_Phase.DataTextField = "Name";
        RDDL_Phase.DataValueField = "ID";
        RDDL_Phase.DataBind();
        RDDL_Phase.Items.Insert(0, new DropDownListItem("--GOO GOO GOO--", "ALL"));
    }
}

is not an option and you must use the control's OnLoad event and rebind it on every postback, I would suggest adding logic to the RDDL_Phase_Load handler for correcting the SelectedIndex when an item is added, for example:
protected void RDDL_Phase_Load(object sender, EventArgs e)
{
    RDDL_Phase.DataSource = this.Get_RDDL_lorenipsum();
    RDDL_Phase.DataTextField = "Name";
    RDDL_Phase.DataValueField = "ID";
    RDDL_Phase.DataBind();
    RDDL_Phase.Items.Insert(0, new DropDownListItem("--GOO GOO GOO--", "ALL"));
    RDDL_Phase.SelectedIndex = RDDL_Phase.SelectedIndex - 1;
}


Regards,
Ivan Danchev
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
DropDownList
Asked by
Pierre
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Pierre
Top achievements
Rank 1
Share this question
or