Accessing Dynamically Created RadButtons on PostBack

3 posts, 1 answers
  1. Tom Rasmussen
    Tom  Rasmussen avatar
    20 posts
    Member since:
    Jan 2009

    Posted 15 May 2012 Link to this post

    I have a user setup form that contains several RadButtons for the individual permissions (checkboxes).  The RadButtons are created at runtime on page init then added to a fieldset:
    var availablePermissions = Permissions.AllPermissions(PermissionCategory.Client);
    foreach (var permission in availablePermissions)
    {
        //create the permission checkbox
        var checkbox = new RadButton
                        {
                            AutoPostBack = false,
                            ToggleType = ButtonToggleType.CheckBox,
                            ButtonType = RadButtonType.ToggleButton,
                            ID = "Permission_" + permission.Name,
                            Value = permission.Name,
                            ToolTip = permission.Description,
                            Text = permission.DisplayName,
                            Checked = workUser.HasPermission(permission)
                        };
            PermissionFieldSet.Controls.Add(checkbox);
    }

    This part of the code works great.  The permissions are added and displayed appropriately.  The problem is now accessing the controls on postback.  Since they were added at runtime and can be varied in number I need to iterate all possible permissions checked.  The original plan was to get any Request.Form elements that matched the ID pattern of "Permission_" like so:
    public bool SaveOrUpdateUser()
    {
        /* Various other user setup fields */
     
        //get all form fields that contain the "Permission_" identifier
        var permissions = Request.Form.AllKeys.Where(key => key.Contains("Permission_")).Select(key => Request.Form[key]).ToList();
    }

    While this call does successfully retrieve the permissions, the information returned is a serialized version of the RadButton control:
    {\"text\":\"ButtonText\",\"value\":\"ButtonValue\",\"checked\":true,\"target\":\"\",\"navigateUrl\":\"\",\"commandName\":\"\",\"commandArgument\":\"\",\"autoPostBack\":false,\"selectedToggleStateIndex\":0,\"readOnly\":false}

    So, how can I deserialize this back into a RadButton to be able to interpret the checked status of each of my permissions?  I know I could manually breakdown the string, but that seems like a workaround at best.

    Thanks.

    Tom
  2. Answer
    Slav
    Admin
    Slav avatar
    1355 posts

    Posted 18 May 2012 Link to this post

    Hello Tom,

    On postback you can iterate through the controls in PermissionFieldSet and get only the ones that are of type RadButton. The following code snippet demonstrates this approach:

    foreach (Control control in PermissionFieldSet.Controls)
    {
        if (control.GetType().Name.ToString() == "RadButton")
        {
            RadButton button = control as RadButton;
            // here is your logic
        }
    }

    I hope this helps. Feel free to contact us again if you run into more difficulties.

    Regards,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tom Rasmussen
    Tom  Rasmussen avatar
    20 posts
    Member since:
    Jan 2009

    Posted 18 May 2012 Link to this post

    I guess the simple answer was staring me in the face.  Thanks.
Back to Top