Databind template items within RadComboBox

3 posts, 0 answers
  1. Mike
    Mike avatar
    2 posts
    Member since:
    Mar 2011

    Posted 07 Mar 2011 Link to this post

    Hello! I have been trying to get data bound to a textbox nested inside an ItemTemplate to no avail. I have attempted to bind it in code behind by looping through items in the list, as well as in the ASP and I am coming up with no answers.

    As you can see below, I have three cells in the ComboBox, and I have no problems loading the combobox items not bound to asp objects.  The problem is that I am unable to bind values to the nested asp:CheckBox and asp:TextBox.

    The text in the section <%#  DataBinder.Eval(Container, "Value")%> is all black, not three tone as is normal in VS2010.
    It's almost as one cannot bind data to ASP items in an ItemTemplate.  Is that correct?

    Here is the ASP.  If this is possible, what am I doing wrong?

    Thanks in advance!

    <telerik:RadComboBox runat="server" ID="ddlHardCopies" HighlightTemplatedItems="true" Width="250px" AllowCustomText="True">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <asp:checkbox runat="server" ID="chkHardCopy" onclick="stopPropagation(event);" Text='<%# DataBinder.Eval(Container, "Value")%>'/>
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtHardCopyQuantity" onclick="stopPropagation(event);" Width="20px" Text='<%# DataBinder.Eval(Container, "Quantity")%>'/>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container, "Text")%>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </telerik:RadComboBox>
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Mar 2011 Link to this post

    Hello Mike,

    I am not quite sure about the way that you used to populate the RadComboBox. I suppose the combobox is bound to a data source form code behind. If that is the case then you must use Container.DataItem instead of Container.

    That is  <%# DataBinder.Eval(Container.DataItem, "Text") %>

    Take look at this documentation also
    Data Binding in Templates

    Hope it helps.
    Thanks,
    Shinu.
  3. Mike
    Mike avatar
    2 posts
    Member since:
    Mar 2011

    Posted 08 Mar 2011 Link to this post

    Thanks, however that didn't quite work for me.

    I actually was able to figure it out using the VB code example on the Data Binding in Templates section you referenced.

    I handled all the databinding for the textbox and checkbox in the code behind like the following:

    Dim cmdz As SqlCommand = New SqlCommand("eProctor.usp_MultiFunction", cnProData)
    cmdz.Parameters.AddWithValue("@Anchor", 20)
    cmdz.Parameters.AddWithValue("@RequisitionID", ReqID)
    cmdz.CommandType = CommandType.StoredProcedure
    Dim daz As New SqlDataAdapter(selectCommand:=cmdz)
    daz.Fill(ds, "HardCopyLocations")
     
    ddlHardCopies.DataSource = ds.Tables("HardCopyLocations")
    ddlHardCopies.DataTextField = "LocationName"
    ddlHardCopies.DataValueField = "LocationID"
    ddlHardCopies.DataBind()
     
    Dim i As Integer = 0
    While i < ddlHardCopies.Items.Count
        For Each item In ddlHardCopies.Items
            Dim vartxt As TextBox = DirectCast(item.FindControl("txtHardCopyQuantity"), TextBox)
            Dim varchk As CheckBox = DirectCast(item.findcontrol("chkHardCopy"), CheckBox)
            vartxt.Text = ds.Tables("HardCopyLocations").Rows(i).Item("Quantity").ToString
            varchk.Checked = CType(ds.Tables("HardCopyLocations").Rows(i).Item("Value").ToString, Byte)
            i = i + 1
        Next
    End While

    Thanks again for pointing me in the right direction!
    Mike-
Back to Top