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

Accessing Dynamically Created RadButtons on PostBack

2 Answers 77 Views
Button
This is a migrated thread and some comments may be shown as answers.
Tom Rasmussen
Top achievements
Rank 1
Tom Rasmussen asked on 15 May 2012, 05:34 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Slav
Telerik team
answered on 18 May 2012, 12:14 PM
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.
0
Tom Rasmussen
Top achievements
Rank 1
answered on 18 May 2012, 03:45 PM
I guess the simple answer was staring me in the face.  Thanks.
Tags
Button
Asked by
Tom Rasmussen
Top achievements
Rank 1
Answers by
Slav
Telerik team
Tom Rasmussen
Top achievements
Rank 1
Share this question
or