RadComboBox in EditItemTemplate of RadGrid not Finding SelectedValue

7 posts, 0 answers
  1. Lee
    Lee avatar
    13 posts
    Member since:
    Jul 2010

    Posted 04 Nov 2010 Link to this post

    Trying to get the selected value from the radcombobox that is in the edit item template of the radgrid.  Also trying to set the selected value when the edit command is selected.  here is the code below. In the update command i can find the control but for some reason the selected value is blank.  What about using the onSelectedIndexChanged?  is there a certain way you have to do it when the radcombobox is within the edit item template?

    Thanks.

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" PageSize="10"
                AllowPaging="true" AllowSorting="true" AllowMultiRowEdit="True" 
                                  AllowMultiRowSelection="true" HorizontalAlign="NotSet" OnItemCommand="RadGrid1_ItemCommand" OnUpdateCommand="RadGrid1_UpdateCommand">
                                  <MasterTableView CommandItemDisplay="top" DataKeyNames="JobId" ClientDataKeyNames="JobId"
                                      EditMode="InPlace" HorizontalAlign="NotSet">
                                      <Columns>
                                          <telerik:GridEditCommandColumn ItemStyle-Width="100px" FooterText="EditCommand footer" UniqueName="EditCommandColumn"
                                              HeaderText="Edit" HeaderStyle-Width="100px" UpdateText="Update">
                                          </telerik:GridEditCommandColumn>
                                          <telerik:GridBoundColumn ItemStyle-Width="50px" HeaderStyle-Width="50px" ReadOnly="true" DataField="JobId" HeaderText="Job Id" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridTemplateColumn DataField="JobCode" HeaderText="Job Code" DataType="System.String">
                                              <ItemTemplate>
                                                  <%#DataBinder.Eval(Container.DataItem, "JobId")%>
                                              </ItemTemplate>
                                              <EditItemTemplate>
                                                  <telerik:RadComboBox runat="server" ID="RadComboBox1" EnableLoadOnDemand="True" DataTextField="Job Code"
                                                      OnItemsRequested="RadComboBox1_ItemsRequested" DataValueField="JobCode" AutoPostBack="false"
                                                      HighlightTemplatedItems="true" Height="140px" Width="100px" DropDownWidth="100px"
                                                      OnSelectedIndexChanged="RadComboBox1_OnSelectedIndexChanged" SelectedValue='<%#Bind("JobId")%>' >
                                                  </telerik:RadComboBox>
                                              </EditItemTemplate>
                                          </telerik:GridTemplateColumn>
                                          <telerik:GridBoundColumn ReadOnly="true" DataField="Description" HeaderText="Description" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridTemplateColumn DataField="JobCodeMatch" HeaderText="Job Code Match"
                                              DataType="System.String">
                                              <ItemTemplate>
                                                  <%#DataBinder.Eval(Container.DataItem, "JobCodeMatch")%>
                                              </ItemTemplate>
                                              <EditItemTemplate>
                                                  <telerik:RadComboBox runat="server" ID="RadComboBox2" EnableLoadOnDemand="True" DataTextField="Job Code Match"
                                                      OnItemsRequested="RadComboBox2_ItemsRequested" DataValueField="JobCodeMatch"
                                                      AutoPostBack="false" HighlightTemplatedItems="True" Height="140px" Width="100px"
                                                      DropDownWidth="100px" OnSelectedIndexChanged="RadComboBox2_OnSelectedIndexChanged" SelectedValue='<%#Bind("JobCodeMatch")%>' >
                                                  </telerik:RadComboBox>
                                              </EditItemTemplate>
                                          </telerik:GridTemplateColumn>
                                          <telerik:GridBoundColumn ReadOnly="true" DataField="LaborText" HeaderText="Labor Flag" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridBoundColumn ReadOnly="true" DataField="EditDate" HeaderText="Edit Date" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridBoundColumn ReadOnly="true" DataField="Editor" HeaderText="Editor" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                          <telerik:GridBoundColumn ReadOnly="true" DataField="Contact" HeaderText="Contact" DataType="System.String">
                                          </telerik:GridBoundColumn>
                                      </Columns>
                                      <EditFormSettings>
                                      </EditFormSettings>
                                  </MasterTableView>
                                  <ClientSettings>
                                      <Selecting AllowRowSelect="True" EnableDragToSelectRows="True" />
                                  </ClientSettings>
                           </telerik:RadGrid>

    Protected Sub OnItemDataBoundHandler(ByVal sender As Object, ByVal e As GridItemEventArgs)
        If e.Item.IsInEditMode Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Session("rcbValue") = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("JobId").ToString()
            If Not (TypeOf e.Item Is IGridInsertItem) Then
                Dim combo As RadComboBox = DirectCast(item.FindControl("RadComboBox1"), RadComboBox)
            End If
        End If
    End Sub
     
    Protected Sub RadComboBox1_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)  
        oData = oPassThru.GetJobCodeIds(Profile.Business) '(Profile.Business)
        Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox)
        comboBox.Items.Clear()
        For Each o As clsAdminJobCodes In oData
            Dim item As New RadComboBoxItem()
            item.Text = o.JobId.ToString()
            item.Value = o.JobId.ToString()
            comboBox.Items.Add(item)
            item.DataBind()
        Next
    End Sub
      
    Protected Sub RadComboBox2_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
        oData = oPassThru.GetJobCodeMatchId(Profile.Business) '(Profile.Business)
        Dim comboBox As RadComboBox = DirectCast(sender, RadComboBox)
        comboBox.Items.Clear()
        For Each o As clsAdminJobCodes In oData
            Dim item As New RadComboBoxItem()
            item.Text = o.JobCodeMatch.ToString()
            item.Value = o.JobCodeMatch.ToString()
            comboBox.Items.Add(item)
            item.DataBind()
        Next
     End Sub
     
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        If (e.CommandName = RadGrid.EditCommandName) Then
            Session("rcbValue") = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("JobId").ToString()
        End If
     End Sub
     
    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
        Dim var3 As String = CType(RadGrid1.Items(0).FindControl("RadComboBox1"), RadComboBox).SelectedValue
        Dim editedItem1 As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim var As String = CType(editedItem1.Cells(4).Controls(1), RadComboBox).SelectedValue
        Dim Var2 As String = CType(editedItem1.Controls(4).FindControl("RadComboBox2"), RadComboBox).SelectedValue
     End Sub
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Nov 2010 Link to this post

    Hello Lee,

    Since you using the ItemsRequested event , you have to set the selected value explicitly in the RagGrid ItemDataBound event. This is because the editform opens too early to bind the RadComboBox in EditItemTemplate.

    Here is the code that i tried for a similar scenario.
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                GridEditableItem edititem = (GridEditableItem)e.Item;
                if ((e.Item is IGridInsertItem))
                {
                    GridDataItem item = (GridDataItem)edititem.OwnerTableView.Items[0] ;
                    DataRowView rowview = (DataRowView)item.DataItem;
                    RadComboBox combo = (RadComboBox)edititem.FindControl("RadComboBox1");
                    combo.Text = rowview ["JobCode"].ToString();
                }
            }
        }


    And the following code will help you in accessing the RadComboBox SelectedValue in UpdateCommand.

    Code:
    Protected Sub RadGrid1_UpdateCommand(source As Object, e As GridCommandEventArgs)
        Dim editedItem1 As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim combo As RadComboBox = DirectCast(editedItem1.FindControl("RadComboBox1"), RadComboBox)
        Dim selected As String = combo.SelectedValue
     
    End Sub

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lee
    Lee avatar
    13 posts
    Member since:
    Jul 2010

    Posted 05 Nov 2010 Link to this post

    Dim item As GridDataItem = DirectCast(edititem.OwnerTableView.Items(0), GridDataItem)

    doesn't get the value of the item...
  5. Lee
    Lee avatar
    13 posts
    Member since:
    Jul 2010

    Posted 05 Nov 2010 Link to this post

    this worked for the selectedvalue set

    Dim value As String = edititem.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("JobId").ToString()
    Dim combo As RadComboBox = DirectCast(edititem.FindControl("RadComboBox1"), RadComboBox)
    combo.Text = value

    sorry that only gets the keyvalue...still working.
    Dim value As String = edititem.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("JobId").ToString()
                    Dim combo As RadComboBox = DirectCast(edititem.FindControl("RadComboBox1"), RadComboBox)
                    combo.Text = valu
  6. Lee
    Lee avatar
    13 posts
    Member since:
    Jul 2010

    Posted 08 Nov 2010 Link to this post

    didn't realize i could use DataKeyNames="JobId,JobCodeMatch" 

    that with e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("JobCodeMatch").ToString

    set the selected value.

    thanks!

  7. Lee
    Lee avatar
    13 posts
    Member since:
    Jul 2010

    Posted 18 Nov 2010 Link to this post

    Protected Sub RadGrid1_UpdateCommand(source As Object, e As GridCommandEventArgs)
        Dim editedItem1 As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim combo As RadComboBox = DirectCast(editedItem1.FindControl("RadComboBox1"), RadComboBox)
        Dim selected As String = combo.SelectedValue
    End Sub

    says that that selectedvalue on the update is empty.

    so not sure why this is happening.
  8. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 24 Nov 2010 Link to this post

    Hi Lee Wilbourn,

    Your case is complex and finding the root cause of the empty selected value would require thorough testing/debugging. Please open a support ticket and send us a working project that shows the issue. We will do our best to find the cause of the error and will write to you and in this forum thread as well.

    All the best,
    Simon
    the Telerik team
    Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017