Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
In my project i am using a data-list to display all the questions and the corresponding options are in radiobutton-list
now the problem is in Datalist-ItemBoundEvent only the first option is binding,rest options are not displaying in the radiobutton list
my asp page is
XML
<asp:DataList ID="dlQuestions" runat="server" CssClass="datalist" OnItemDataBound="dlQuestions_ItemDataBound">
                <ItemTemplate>
                    <table>
                        <colgroup>
                            <col class="column1" style="" />
                            <col class="column2" style="" />
                        </colgroup>
                        <tr>
                            <td>
                                <span class="heading4"><%#Container.ItemIndex+1 %>.</span>
                                <asp:HiddenField ID="hf1" runat="server" Value='<%#Eval("TrainingExamQuestionBankId")%>' />
                            </td>
                            <td>
                                <span class="heading4"><%#Eval("Question") %></span>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td class="Label">
                                <asp:RadioButtonList runat="server" ID="rbOptions">
                                </asp:RadioButtonList>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>


and my cod behind is like
C#
 protected void dlQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("rbOptions");
                HiddenField hdn = (HiddenField)e.Item.FindControl("hf1");
                int QuestionId = Convert.ToInt32(hdn.Value);
                var query = (from a in TraningExamQuestionBank
                             where a.TrainingExamQuestionBankId == QuestionId
                             select new
                             {
                                 a.OptionA,
                                 a.OptionB,
                                 a.OptionC,
                                 a.OptionD,
                                 a.OptionE,
                             }).ToList();
                DataTable dt = ConvertToDataTable(query);
                RadioButtonList1.DataSource = dt;
                RadioButtonList1.DataTextField = dt.Columns[0].ToString();
                RadioButtonList1.DataValueField = dt.Columns[0].ToString();
                RadioButtonList1.DataBind();
            }
        }
public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable Table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                Table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = Table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                Table.Rows.Add(row);
            }
            return Table;
        }

please help....!!
Posted
Comments
DamithSL 21-Apr-14 2:06am    
what is the type of OptionA? string or bool?
Amit Kumar143 21-Apr-14 2:08am    
it is varchar

1 solution

it seems you have one record with all the options and it will bind in to a one item in radio button list. what you can do is; get the record and create list with all the options and bind it to radio button list as below

C#
var item= (from a in TraningExamQuestionBank
                             where a.TrainingExamQuestionBankId == QuestionId
                             select new
                             {
                                 a.OptionA,
                                 a.OptionB,
                                 a.OptionC,
                                 a.OptionD,
                                 a.OptionE,
                             }).FirstOrDefault();
if(item !=null)
{
        List<string> items = new List<string>(){ item.OptionA, item.OptionB, item.OptionC, item.OptionD,         item.OptionE};
        RadioButtonList1.DataSource = items;  
        RadioButtonList1 DataBind(); 
}
 
Share this answer
 
v3
Comments
Amit Kumar143 21-Apr-14 2:46am    
it's working fine
Thanks @Damith Weerasinghe...!!
Can u tell me how to retrieve the selected options because i have to save the selected options in database.
DamithSL 21-Apr-14 3:02am    
in which event you want to get the value? haven't you try as same as adioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("rbOptions"); and then use property like SelectedValue or SelectedItem of RadioButtonList1?
Amit Kumar143 21-Apr-14 3:13am    
i have a Button Event,in which i have save the values.
in my database situation,when user select 1st option and click save then "A" is save in database
when user click 2nd option and click save then "B" is save in database
when user click 3rd option and click save then "C" is save in database
when user click 4th option and click save then "D" is save in database
when user click 5th option and click save then "E" is save in database

Do you have any idea how can i achieve it?
Help in this..!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900