Thursday, March 10, 2005

Placing Radio buttons in Datagrid control

Here the challenge we faced was that, from a given set of radio buttons in datagrid, we can select only one radio button. This feature is for the purpose, to select a row from DataGrid. Radio buttons behave as a single group based on their GroupName property. When put inside DataGrid, DataGrid generates a unique name for each radio button. This causes each radio button to operate independently of others.

In order to solve the problem mentioned above, we create a template column and put a Label web control in it. In the ItemDataBound event of the DataGrid we wrote code that emits 'Input' element with type as Radio and set its Name attribute. This will display the grid with radio buttons in the column. You can select any one of the radio button at a time. This solved our requirement.

In order to retrieve the selected radio button, since we are not using RadioButton web control, we can not directly refer it as usual. Instead we can use ‘Request.Form’ collection. We can directly retrieve the selected radio button using this collection as in traditional ASP.

Programming at its best....

Dropdown Controls in Repeater Web Control

A repeater control which contains around 4 to 5 rows and in each row, there will be a dropdown control. On selection of dropdown, the value in selected dropdown has to be retrieved, stating which row and which dropdown selected. We thought about various options to resolve this issue, and nothing seems to work and finally we managed to find this roundabout for this issue. Steps are as below:

1. In the Itemdatabound event of the repeater control, create the SelectedIndexChanged event of the dropdown.

DropDownList MyList = (DropDownList) e.Item.FindControl ("CN");
MyList.SelectedIndexChanged += new EventHandler (CN_SelectedIndexChanged);

2. On selection of dropdown in repeater control, in the corresponding SelectedIndexChanged event, typecast the sender as dropdown and retrieve the selected item value.

Private void CN_SelectedIndexChanged (object sender, System.EventArgs e)
DropDownList CL = (DropDownList) sender;
StrChildNumber = CL.SelectedItem.Value;

In Datagrid, for fetching the rowindex of the selected dropdownlist,
TableCell cell = list.Parent as TableCell;
DataGridItem item = cell.Parent as DataGridItem
int index = item.ItemIndex;
string content = item.Cells[0].Text;

The index variable fetches the rowindex, and
the Content variable fetches the content in selected dropdown


For retrieving the selected row index of repeater control, it is that we have appended the row index with the dropdown value with a delimiter and later on retrieving the dropdown value, we can split the dropdown value based on the delimiter and selected row index.

Happy Programming...

This page is powered by Blogger. Isn't yours?