I have a web page with an ASP datagrid which populates address information. This also has a radio button to allow the user to select a row of the grid as their address.
Selecting a column works fine and I can get back the ID of the row. What I would like to do is on rendering of the datagrid, the user will already have an address ID so the corresponding row should be selected on render. How can I do this in the code behind file?
The address id will be an Int called selectedAddress. I have had a search and cant find anything that explains how this can be done. I realise I will need to go through each data grid row and check the ID value, but I am not sure how to do this. Any help would be great.
Thanks.
NamritaPosted Apr 15, 2009, 7:54 AM
I hope you can achieve
this by placing bit of logic within the ItemDataBound event.Also you need to replace the html radio button with asp:
RadioButton control. protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e){
RadioButton rdAddressID = (RadioButton)e.Item.FindControl("RdButton");// "RdButton" is the ID of the radio button control string ID = e.Item.Cells[7].Text; //here cells[7] is the index of the Id column int SelectedAddressId = "Some Value";//Which is already available as mentioned by you if (rdAddressID != null){
if (SelectedAddressId == ID.ToString()){
rdAddressID.Checked =
true;}
}
}
Hope this helps.