locked
Get The Selected Role On Edit RRS feed

  • Question

  • User-698989805 posted

    Hello friends! I've a DropDownList - ASP.NET WebForm where it filled up with roles and did it like the below from database:

    protected void Page_Load(object sender, EventArgs e)
    {
    LoadDropDownBox();
    }

    public void LoadDropDownBox() { ddlUserRole.DataSource = aDbOperations.GetRoles(); //List of objects here ddlUserRole.DataTextField = "roleName"; ddlUserRole.DataValueField = "roleId"; ddlUserRole.DataBind(); }

    It works fine. Now my requirement is to get the selected role of a user when I edit or update user details. So say a user has role Admin, then while editing, the admin role should be selected by default along with other values. So I tried something like this:

    protected void Page_Load(object sender, EventArgs e)
    {
       foreach (var item in aDbOperations.GetUserWithId(id)) //Passing query string here
       {
          foreach (ListItem item2 in ddlUserRole.Items)
          {
              if (item2.Text == item.roleName) //If role matches
              {
                 ddlUserRole.SelectedItem.Value = item.roleName; //Get the selected role name by default while editing user details
              }
          }
       }
       
       LoadDropDownBox();
    }

    Though it didn't work and not getting the value selected by default from database. Anything missed here?

    Friday, November 30, 2018 8:58 AM

All replies

  • User475983607 posted

    The code bind the dropdown on every request.  Bind the control on the first load which is a standard ASP,NET pattern.

    protected void Page_Load(object sender, EventArgs e)
    {
    	if(!Page.IsPostBack) 
    	{
    		LoadDropDownBox();
    	}
       
    }

    The second block of code sets the selected value before the control is bound which reset the selected value.

    Please review your code.  Use the Visual Studio debugger to step through the logic and verify the logic functions as expected.

    Friday, November 30, 2018 12:07 PM
  • User-893317190 posted

    Hi TechView,

    It seems that you want  when item2.Text == item.roleName, make the listitem selected.

    What's the valuefield of your dropdownlist ?Is it your roleName?

    If so,please use ddlUserRole.SelectedValue property instead of SelectedItem property.

    For example , If your dropdown has two listitem {"role1":"role1value"      ,     "role2":"role2value"} and its selecteditem is role1.

    When you use this way to select role2 ,ddlUserRole.SelectedItem.Value="role2value",it will be  {"role1":"role2value"     ,"role2":"role2value"}.

    It just changes the value of the selectedItem instead of change the selected item.

    If you want to use loop , you could try

     if (item2.Text == item.roleName) //If role matches
              {
                 item2.selected = true; //Get the selected role name by default while editing user details
              }

    Best regards,

    Ackerly Xu

    Monday, December 3, 2018 8:15 AM