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

Binding RadioButtonList in FormTemplate

3 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Derek
Top achievements
Rank 1
Derek asked on 21 Nov 2008, 07:21 PM
Can someone give me an example of how to bind a RadioButtonList in a FormTemplate?  I know how to do checkboxes and drop down lists but can't seem to figure out RadioButtonLists.  Thanks.



Update: Nevermind, I figured it out.  :)

3 Answers, 1 is accepted

Sort by
0
peter
Top achievements
Rank 1
answered on 09 Dec 2008, 07:21 PM
Derek

Perhaps you could share how you did this with me ? A small code sample will suffice.

Much appreciated
Hans
0
Shinu
Top achievements
Rank 2
answered on 10 Dec 2008, 04:55 AM
Hi Peter,

You can place the RadioButtonList in the FormTemplate and bind it in the code behind as shown below.

ASPX:
                 <FormTemplate> 
                      <asp:RadioButtonList ID="RadioButtonList1"    runat="server"
                     </asp:RadioButtonList> 
                </FormTemplate> 


C#:
  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode)) 
        { 
            GridEditableItem edititem = (GridEditableItem)e.Item; 
            RadioButtonList radbtnlist = (RadioButtonList)edititem.FindControl("RadioButtonList1"); 
             
            Hashtable mycountries = new Hashtable(); 
            mycountries.Add("N""Norway"); 
            mycountries.Add("S""Sweden"); 
            mycountries.Add("F""France");   
            mycountries.Add("I""Italy"); 
            radbtnlist.DataSource = mycountries; 
            radbtnlist.DataValueField = "Key"
            radbtnlist.DataTextField = "Value"
            radbtnlist.DataBind(); 
        } 
    
         
    } 


Thanks
Shinu
0
Roger
Top achievements
Rank 1
answered on 12 Mar 2009, 06:49 AM
Hi Shinu,

that piece of information on loading a radiobuttonlist was great & helped me greatly but I had problems accessing the radiobuttonlist  as it wasn't bound to the original data.

So for anyone out there that has the same problem use the following code :

 

    
        Dim
rb As RadioButtonList = CType(editedItem.FindControl("RadioButtonList1"), RadioButtonList)

from the RadGrid_InsertCommand or RadGrid_EditCommand event.

Then use the rb.SelectedValue property to get the result of the RadioButtonList.
Note: if no radiobuttons are selected the resultant value will be an empty string (the normal non-select is -1 in a form) so check for the empty string before getting the value .

Regards

 

Tags
Grid
Asked by
Derek
Top achievements
Rank 1
Answers by
peter
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Roger
Top achievements
Rank 1
Share this question
or