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

Inserting Radio Button into dynamic grid

7 Answers 896 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 09 Dec 2008, 09:59 PM
I'm creating a form/grid based on the user's selection from a drop down list. The grid could contain anywhere from a minimum of 10 rows up to 20 rows of questions depending on the users previous choice.

The grid would contain the following:

Column 1              Column 2
Yes No N/A           Question 1
Yes No N/A           Question 2

Submit Button


I need to insert a 3 radio buttons into a column 1. I also need to programmically name the radio control for each row so I can take score and write it to a different table. The page would also have a submit button that store the answers into various tables.

I can display the questions based on the users choice in a grid but I cannot figure how to programmically display the radio buttons.

Any suggestions?

Thanks,
Joe

7 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 12 Dec 2008, 12:26 PM
Hi Joe,

You can create the whole control structure dynamically, as well as a template column (in pageInit), which can host the buttons. This article contains additional information on one possible approach.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Joe
Top achievements
Rank 2
answered on 01 Jan 2009, 07:15 PM
I've figured out how to add the radio buttons to a grid that is populated by a sql table (thanks to telerik and my friend Jay) but now I cannot figure out how to dynamically set the ID of the button list to the value of a hidden cell within the same row.

For example, my grid looks like this. ( " _ " = a radio button)

1st Column (Visible)    2nd Column (Hidden)       3rd Column (Visible)   4th Column (Hidden)         
_ Yes _ No _ N/A          1 (Question ID)                 Question                     .7 (point value)

I want the RadioButtonList ID to equal the value of the second column for that row so when I do a SQL table insert of one table and a SQL table update of another table (after the user clicks submit) I can insert the RadionButtonList value the user selected when I step thru each row.

So the first row's RadioButtonList ID would be ID="1", the 2nd Column's value of that row. The second column's value will not always be a number that is 1 + the previous row's value as the value of column two changes depending on what data populates the grid based on the previous choices made by the user.

Here's the code I have now...

        <telerik:RadGrid ID="RadGrid1" runat="server" Skin="Gray"   
            DataSourceID="SqlDataSourceQuestions" GridLines="None">  
                <HeaderContextMenu EnableTheming="True">  
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>  
                </HeaderContextMenu>  
 
                <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSourceQuestions">  
                <RowIndicatorColumn>  
                <HeaderStyle Width="20px"></HeaderStyle>  
                </RowIndicatorColumn>  
 
                <ExpandCollapseColumn>  
                <HeaderStyle Width="20px"></HeaderStyle>  
                </ExpandCollapseColumn>  
                    <Columns>  
                      
                    <telerik:GridTemplateColumn UniqueName="QuestionSelection" HeaderText="Select One" HeaderStyle-HorizontalAlign="Center" ItemStyle-Wrap="false"   
                    FooterStyle-HorizontalAlign="Center"  >    
                                        <ItemTemplate>  
                                            <asp:RadioButtonList RepeatDirection="Horizontal" ID="Question_ID" runat="server">  
                                            <asp:ListItem>Yes</asp:ListItem>  
                                            <asp:ListItem>No</asp:ListItem>  
                                            <asp:ListItem>N/A</asp:ListItem>  
                                            </asp:RadioButtonList>   
                                        </ItemTemplate>    
                                      </telerik:GridTemplateColumn>    
 
                               
                        <telerik:GridBoundColumn DataField="Question_ID" DataType="System.Int32"   
                            HeaderText="Question_ID" ReadOnly="True" SortExpression="Question_ID"   
                            UniqueName="Question_ID" Display="false">  
                        </telerik:GridBoundColumn>  
                        <telerik:GridBoundColumn DataField="Question_Text" HeaderText="Did the Agent do the following....?" SortExpression="Question_Text" UniqueName="Question_Text">  
                        </telerik:GridBoundColumn>  
                        <telerik:GridBoundColumn DataField="Point_Value" DataType="System.Decimal"   
                            HeaderText="Point_Value" SortExpression="Point_Value" UniqueName="Point_Value" Display="false">  
                        </telerik:GridBoundColumn>  
                     </Columns>  
                </MasterTableView>  
 
                <FilterMenu EnableTheming="True">  
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>  
                </FilterMenu>  
                        </telerik:RadGrid>  
                        <asp:SqlDataSource ID="SqlDataSourceQuestions" runat="server"   
                            ConnectionString="<%$ ConnectionStrings:eonPortalCS %>"   
                            SelectCommand="SELECT [Question_ID], [Question_Text], [Point_Value] FROM [Questions] WHERE (([Active] = @Active) AND ([Discipline] = @Discipline)) ORDER BY [Question_ID]">  
                            <SelectParameters>  
                                <asp:Parameter DefaultValue="Yes" Name="Active" Type="String" />  
                                <asp:ControlParameter ControlID="DropDownList1" Name="Discipline"   
                                    PropertyName="SelectedValue" Type="String" />  
                            </SelectParameters>  
                        </asp:SqlDataSource> 

So I need the RadioButtonList ID to change automatically. I tried using <% %> code but it doesn't allow it there.

Any suggestions ?

Joe

PS. Also, in my code behind, when the Submit Button is clicked (it isn't in the code yet), how do I step thru each row that was generated by the databind so I can get the value of the RadioButtonList (by it's unique ID) and update another SQL table with the users button selection?



0
Yavor
Telerik team
answered on 05 Jan 2009, 07:20 AM
Hi Joe,

Indeed, using a code block for the id is not allowed, it will generate an error.
Another possible way in this case would be to use the PreRender event handler, and access each row (RadGridInstance.MasterTableView.Items), and the respective cell, to get a reference to the id, and the radio button control. You can use the same approach to iterate through the items in the grid, to get the button value(s), and populate the SQL routine that you mentioned.
I hope this information helps.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Joe
Top achievements
Rank 2
answered on 05 Jan 2009, 03:38 PM
Thanks for the reply. I think I also found a solution using a FOR EACH statement that runs when the user clicks the submit button, here's  code snippet:

 For Each dataItem As GridDataItem In RadGrid1.Items  
            Dim QuestionScore As Decimal 
            Dim QuestionID As String = dataItem.Item("Question_ID").Text  
 
            Dim PointScore = CType(dataItem.FindControl("ScoreChoice"), RadioButtonList)  
 
.... If statement that evaluates the value of the RadioButtonList ....  
 
.... SQL statement that updates the table thru each loop ....  
 
NEXT  
 
END SUB  
 

Will this work? So far, it seems to provide the information that I need.

Thanks,
Joe
0
Accepted
Yavor
Telerik team
answered on 06 Jan 2009, 08:54 AM
Hi Joe,

The code is correct and should cater for the functionality that you mentioned.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Milind
Top achievements
Rank 1
answered on 21 Apr 2012, 09:28 AM
Hi Joe,

I am Milind.

I am working on the survey building software. My problem is, I am not able to make user select only one radio button from five radio buttons provided per row. Also I am not able to refer to the selected radio button in the code behind. Can you help me by posting the solution ?

Thanks & Regards,

Milind
0
Princy
Top achievements
Rank 2
answered on 23 Apr 2012, 11:24 AM
Hi Milind,

Try using RadioButton List for selecting only one at a time and use and access selected RadioButton on an external Button click.

ASPX:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" >
    <asp:ListItem>A</asp:ListItem
    <asp:ListItem>B</asp:ListItem>
    <asp:ListItem>C</asp:ListItem>
    <asp:ListItem>D</asp:ListItem>
    <asp:ListItem>E</asp:ListItem>
</asp:RadioButtonList>
 
<asp:Button ID="button1" runat="server" onclick="button1_Click" />

C#:
protected void button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        RadioButtonList rdl = (RadioButtonList)item.FindControl("RadioButtonList1");
        string selected = rdl.SelectedItem.Value;
    }
}

Please elaborate your scenario if this doesn't help.

Thanks,
Princy.
Tags
Grid
Asked by
Joe
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Joe
Top achievements
Rank 2
Milind
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or