Change selected value of dropdown list inside nested relay

I tried changing the selected dropdownlist value from C # code:

<repeater >
   <repeater> 
      <dropdown>

and I add the data source on the aspx page in the drop-down list everything works fine .. but I want to change the selected onload value of the drop-down list to show the user the previous selected name when editing the table

protected void PrepairDropDownList(object sender,EventArgs e)
    {
        shiftrepeater.DataBind();
        if (shiftrepeater.Items.Count > 0)
        {
            for (int shiftcount = 0; shiftcount < shiftrepeater.Items.Count; shiftcount++)
            {
                Repeater temp = (Repeater)shiftrepeater.Items[shiftcount].FindControl("saturdayrepeater");
                if (temp.Items.Count > 0)
                {
                    for (int count = 0; count < temp.Items.Count; count++)
                    {
                        DropDownList ds = (DropDownList)temp.Items[count].FindControl("userdropdown");
                        HiddenField hf = (HiddenField)temp.Items[count].FindControl("hiddenid");//contain the id if the field 
                        SarcShiftUser user = CRUD<SarcShiftUser>.Get(int.Parse(hf.Value)); //a method to select a user with a specific id and add it to object from class sarcshiftuser

                        if (user.id == 0)
                            ds.SelectedValue = "";
                        else
                        {
                            ds.SelectedValue = user.user_id + "";

                        }
                    }
                }

            }
        }
        shiftrepeater.DataBind();
    }

I add this method to the Onload repeater: but nothing has changed, and the previous name did not show

PS: the user object is correct and user.user_id is also true PS 2: I tried adding ds.DataBind (); after changing the selected, but here is the error:

System.ArgumentOutOfRangeException: 'userdropdown' has a SelectedValue which is invalid because it does not exist in the list of items.
+3
source share
1 answer

SelectedItem DropdownList - SelectedIndex :

ds.SelectedIndex = ds.Items.IndexOf(ds.Items.FindByValue(user.user_id.ToString()));
+1

All Articles