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

Set Radiobuttonlist value on insert

1 Answer 184 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 21 Apr 2010, 04:59 PM
I have tried the links to setting values for bound items in a edit/insert form template but cannot get it to work. I have a radiobuttonlist in my custom edit form and when I try to insert a new record I get the

'RadioButtonList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
error.

This radiobuttionlist is bound to a sql data field called "publishgroup"  to get its selected value however I have tried and tried to set the selected value on insert but cannot get it set. Can you provide a code sample to find the control and set the value when I go into insert mode.

Thanks

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Apr 2010, 07:17 AM
Hello Robert,

You can access the control in ItemDataBound event and set the SelectedValue to desired. I hope the following code sample will help you.

aspx:

 <EditFormSettings EditFormType="Template"
            <FormTemplate> 
                        <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataSourceID="SqlDataSource2" 
                            DataTextField="value" DataValueField="value"
                        </asp:RadioButtonList> 
           </FormTemplate> 
  </EditFormSettings> 

CS:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted) 
        { 
            GridEditFormInsertItem insertitem = (GridEditFormInsertItem)e.Item; 
            RadioButtonList rdlist = (RadioButtonList)insertitem.FindControl("RadioButtonList1"); 
            rdlist.SelectedValue = "value2";
         } 
    } 

Regards,
Shinu.
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or