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

How to databind a RadioButtonList inside of a RadListView

3 Answers 324 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Hilton
Top achievements
Rank 1
Hilton asked on 28 May 2015, 10:36 PM

Does anybody have an example on how to databind a RadioButtonList inside of a RadListView? I have been trying for hours so any help will be much appreciated.

Here is a small code snippet:

<telerik:RadListView ID="rlvQuestions" DataSourceID="EntityDataSource1" runat="server"
    ItemPlaceholderID="QuestionsContainer" DataKeyNames="EMPLID,Q_ORDER,FISCAL_YEAR">
    <LayoutTemplate
            <fieldset id="FiledSet1"
                    <legend>Questions</legend
                        <div class="RadListView RadListView_<%# Container.Skin %>">
                        <asp:PlaceHolder ID="QuestionsContainer" runat="server"></asp:PlaceHolder
                        </div
                </fieldset
        </LayoutTemplate
                                  
        <ItemTemplate
            <fieldset>
                <table style="width:100%; border:none;">
                <tr>
                <td style="width:60%; vertical-align:top; padding-top:2px; text-align:left">
                    <div style="width:520px; margin:0 0;">
                        <div style="width:20px; height:auto; text-align:left; float:left; display:inline;">
                            <asp:Label ID="lblQNum" CssClass="SectionLabel" runat="server" Text='<%# Bind("Q_ORDER")%>'></asp:Label>.
                        </div>
                        <div style="width:500px; height:auto; text-align:left; float:left; display:inline;">
                            <asp:Label ID="Label2" CssClass="PopupTeal" runat="server" Text='<%# Bind("QUESTION")%>' /><br />
                        </div>
                    </div>
                </td>
                <td style="width:40%; text-align:left">
                    <asp:Label ID="lblbAssessment" CssClass="DisplayTextBold"
                        runat="server" Text="" />
                    <asp:RadioButtonList ID="rbAssessment" CssClass="DisplayTextBold"
                        RepeatColumns="2" RepeatDirection="Vertical"
                        RepeatLayout="Table" runat="server">
                        <asp:ListItem Value="Exceeded Goals"  />
                        <asp:ListItem Value="Achieved Goals" />
                        <asp:listitem Value="Did Not Meet Goals" />
                        <asp:ListItem Value="N/A" />
                    </asp:RadioButtonList>
                </td>
                </tr>

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 02 Jun 2015, 01:23 PM
Hello Hilton,

For achieving the desired result you can include the data field that will hold the selected value from the RadioButtonList control in the DataKeyNames collection (so you can easily retrieve it) and then you can handle the server-side OnItemDataBound event, get reference to the RadioButtonList control and set its SelectedValue property:
<telerik:RadListView ID="rlvQuestions" OnNeedDataSource="rlvQuestions_NeedDataSource" runat="server" OnItemDataBound="rlvQuestions_ItemDataBound"
    ItemPlaceholderID="QuestionsContainer" DataKeyNames="TestValue">
    <LayoutTemplate>
        <fieldset id="FiledSet1">
            <legend>Questions</legend>
            <div class="RadListView RadListView_<%# Container.Skin %>">
                <asp:PlaceHolder ID="QuestionsContainer" runat="server"></asp:PlaceHolder>
            </div>
        </fieldset>
    </LayoutTemplate>
    <ItemTemplate>
        <fieldset>
            <table style="width: 100%; border: none;">
                <tr>
                    <td style="width: 60%; vertical-align: top; padding-top: 2px; text-align: left">
                         
                    </td>
                    <td style="width: 40%; text-align: left">
                        <asp:Label ID="lblbAssessment" CssClass="DisplayTextBold"
                            runat="server" Text="" />
                        <asp:RadioButtonList ID="rbAssessment" CssClass="DisplayTextBold"
                            RepeatColumns="2" RepeatDirection="Vertical"
                            RepeatLayout="Table" runat="server">
                            <asp:ListItem Value="Exceeded Goals" />
                            <asp:ListItem Value="Achieved Goals" />
                            <asp:ListItem Value="Did Not Meet Goals" />
                            <asp:ListItem Value="N/A" />
                        </asp:RadioButtonList>
                    </td>
                </tr>
            </table>
        </fieldset>
    </ItemTemplate>
</telerik:RadListView>

And the code-behind:
protected void rlvQuestions_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("TestValue", typeof(string));
    table.Rows.Add(1, "Exceeded Goals");
    table.Rows.Add(2, "Achieved Goals");
 
 
    (sender as RadListView).DataSource = table;
}
 
protected void rlvQuestions_ItemDataBound(object sender, RadListViewItemEventArgs e)
{
    if (e.Item is RadListViewDataItem)
    {
        RadListViewDataItem dataItem = e.Item as RadListViewDataItem;
        string testValue = dataItem.GetDataKeyValue("TestValue").ToString();
        RadioButtonList buttonList = dataItem.FindControl("rbAssessment") as RadioButtonList;
        buttonList.SelectedValue = testValue;
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Hilton
Top achievements
Rank 1
answered on 02 Jun 2015, 01:49 PM

Thank you very much for your reply. I found that the code below works quite well with Entity Framework as the datasource. Would your approach offer any benefits over this code? Just curious so that I have the best possible solution. :-)

 

Protected Sub rlvQuestions_ItemDataBound(sender As Object, e As RadListViewItemEventArgs) Handles rlvQuestions.ItemDataBound
  
        ' Get the assessment value for each row in the RadListView and set the RadioButtonList value
  
        Dim _rbl As RadioButtonList = CType(e.Item.FindControl("rbAssessment"), RadioButtonList)
        Dim _item As RadListViewDataItem = CType(e.Item, RadListViewDataItem)
        ' convert the current row data to a entity framework VW_APPS_APPRAISALS_ADD_SUPPL_ADMIN class so we can read the data
        Dim _rowView As VW_APPS_APPRAISALS_ADD_SUPPL_ADMIN = CType(_item.DataItem, VW_APPS_APPRAISALS_ADD_SUPPL_ADMIN)
  
        ' set the row's radio button value
        Try
            _rbl.SelectedValue = _rowView.ASSESSMENT.ToString()
        Catch ex As Exception
            _rbl.SelectedValue = String.Empty
        End Try
  
    End Sub
Thanks,

 

Hilton

0
Konstantin Dikov
Telerik team
answered on 05 Jun 2015, 09:29 AM
Hi Hilton,

The approach where you are using the DataItem is more suitable in the context of Entity and will allow you to avoid adding the DataField to the DataKeyNames collection of the MasterTableView, so I would suggest that you use it instead of the one from my previous post.


Best Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ListView
Asked by
Hilton
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Hilton
Top achievements
Rank 1
Share this question
or