1
Reply

Pre-selecting a datagrid Radio button using codebehind file.

david heath

david heath

Apr 15 2009 4:47 AM
5.6k
Hi I hope you can help.

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.

<asp:DataGrid runat="server" ID="orgaddressdg" AutoGenerateColumns="false" >
<Columns>
<asp:BoundColumn DataField="AddressLine1" HeaderText="Address Line One"></asp:BoundColumn>
<asp:BoundColumn DataField="AddressLine2" HeaderText="Address Line Two"></asp:BoundColumn>
<asp:BoundColumn DataField="AddressLine3" HeaderText="Address Line Three"></asp:BoundColumn>
<asp:BoundColumn DataField="PostCode" HeaderText="Postcode"></asp:BoundColumn>
<asp:BoundColumn DataField="CountryName" HeaderText="Country"></asp:BoundColumn>
<asp:BoundColumn DataField="StatusDescription" HeaderText="Status"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Select One">
<ItemTemplate>
<input name="addr_radio_button" type="radio" value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

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.

Answers (1)