POSSIBLE BUG: RadComboBox in UserControl - problem with binding data via RadGrid ItemDataBound event

3 posts, 1 answers
  1. Ignjat
    Ignjat avatar
    13 posts
    Member since:
    Aug 2011

    Posted 31 Aug 2011 Link to this post

    Hello!

    I'm having some serious issues with RadComboBox which behaves erratically. The problem is I can't bind the data from ItemDataBound event of the RadGrid to the RadComboBox that is inside the UserControl. I always get errors that there is something wrong with binding even though it should work properly.

    Here are the code snippets:

    TicketUserControl.ascx
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TicketUserControl.ascx.cs"
        Inherits="IDE_Ticketing_System__Telerik_.UserControls.TicketUserControl" %>
    <table>
        <tr>
            <td>
                Nadređeni ticket:
            </td>
            <td colspan="3">
                <telerik:RadComboBox ID="rcbNadredeniTicket" DataTextField="OpisTicketa" DataValueField="idTicket"
                    AppendDataBoundItems="true" Text='<%# DataBinder.Eval(Container, "DataItem.idNadredeniTicket") %>'
                    runat="server" Width="455px">
                </telerik:RadComboBox>
            </td>
        </tr>
        <tr>
            <td>
                Asset:
            </td>
            <td colspan="3">
                <telerik:RadComboBox ID="rcbAsseti" AppendDataBoundItems="true"
                    runat="server" Width="455px">
                </telerik:RadComboBox>
            </td>
        </tr>
        <tr>
            <td>
                Opći prioritet:
            </td>
            <td>
                <asp:TextBox ID="txtOpciPrioritet" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.OpciPrioritet") %>'></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Tip:
            </td>
            <td>
                <telerik:RadComboBox ID="rcbTip" Text='<%# DataBinder.Eval(Container, "DataItem.Tip") %>'
                    runat="server">
                </telerik:RadComboBox>
            </td>
        </tr>
        <tr>
            <td>
                Status:
            </td>
            <td>
                <telerik:RadComboBox ID="rcbStatus" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'
                    runat="server">
                </telerik:RadComboBox>
            </td>
        </tr>
        <tr>
            <td>
                Datum prijave:
            </td>
            <td>
                <asp:TextBox ID="txtDatumPrijave" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DatumPrijave") %>'></asp:TextBox>
            </td>
            <td>
                Vrsta prijave:
            </td>
            <td>
                <telerik:RadComboBox ID="rcbVrstaPrijave" Text='<%# DataBinder.Eval(Container, "DataItem.VrstaPrijave") %>'
                    runat="server">
                </telerik:RadComboBox>
            </td>
        </tr>
        <tr>
            <td>
                Prioritet:
            </td>
            <td>
                <telerik:RadComboBox ID="rcbPrioritet" Text='<%# DataBinder.Eval(Container, "DataItem.Prioritet") %>'
                    runat="server">
                </telerik:RadComboBox>
            </td>
            <td>
                Deadline:
            </td>
            <td>
                <asp:TextBox ID="txtDeadline" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DatumDo") %>'></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Opis:
            </td>
            <td colspan="3">
                <asp:TextBox ID="txtOpis" runat="server" TextMode="MultiLine" Width="455px" Height="200px"
                    Text='<%# DataBinder.Eval(Container, "DataItem.Opis") %>'></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Zatvoren ticket:
            </td>
            <td>
                <asp:CheckBox ID="chkZatvoren" runat="server" Checked='<%# CheckNull(DataBinder.Eval(Container, "DataItem.Zatvoren")) %>' />
            </td>
        </tr>
        <tr>
            <td>
                Opis zatvorenog ticketa:
            </td>
            <td colspan="3">
                <asp:TextBox ID="txtOpisZatvorenog" runat="server" TextMode="MultiLine" Width="455px"
                    Height="200px" Text='<%# DataBinder.Eval(Container, "DataItem.OpisZatvoren") %>'></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnUpdate" Text="Spremi" runat="server" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'>
                </asp:Button>
                <asp:Button ID="btnInsert" Text="Spremi" runat="server" CommandName="PerformInsert"
                    Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:Button>
                  
                <asp:Button ID="btnCancel" Text="Odustani" runat="server" CausesValidation="False"
                    CommandName="Cancel"></asp:Button>
            </td>
        </tr>
    </table>

    TvrtkaWebForm.aspx (the one with RadGrid)
    <telerik:RadGrid ID="gvTicketi" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" CellSpacing="0" GridLines="None"
        onneeddatasource="gvTicketi_NeedDataSource"
        onitemdatabound="gvTicketi_ItemDataBound"
        oninsertcommand="gvTicketi_InsertCommand">
        <MasterTableView DataKeyNames="idTicket" CommandItemDisplay="TopAndBottom" InsertItemPageIndexAction="ShowItemOnCurrentPage">
            <CommandItemSettings AddNewRecordText="Dodaj novi ticket" RefreshText="Prikaži sve tickete" />
            <Columns>
                <telerik:GridBoundColumn UniqueName="idTicket" HeaderText="Ticket" DataField="idTicket">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="idKontakt" HeaderText="Kontakt" DataField="Kontakt">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="idManager" HeaderText="Manager" DataField="idManager">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Opis" HeaderText="Opis" DataField="Opis">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="TicketNumber" HeaderText="Broj ticketa" DataField="TicketNumber">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="DatumPrijave" HeaderText="Datum prijave" DataField="DatumPrijave" DataFormatString="{0:dd.MM.yyyy.}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="VrstaPrijave" HeaderText="Vrsta prijave" DataField="VrstaPrijave">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Tip" HeaderText="Tip" DataField="Tip">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Status" HeaderText="Status" DataField="Status">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Prioritet" HeaderText="Prioritet" DataField="Prioritet">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="OpciPrioritet" HeaderText="Opći prioritet" DataField="OpciPrioritet">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="Deadline" HeaderText="Deadline" DataField="DatumDo" DataFormatString="{0:dd.MM.yyyy.}">
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn UniqueName="Zatvoren" HeaderText="Zatvoren" DataField="Zatvoren">
                </telerik:GridCheckBoxColumn>
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete"
                    ConfirmDialogType="RadWindow" ConfirmText="Brisanje ticketa!" />
            </Columns>
            <EditFormSettings UserControlName="UserControls/TicketUserControl.ascx" EditFormType="WebUserControl">
                <EditColumn UniqueName="EditCommandColumn1">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>

    TvrtkaWebForm.aspx.cs:
    protected void gvTicketi_ItemDataBound(object sender, GridItemEventArgs e)
    {
        int idFirma = Convert.ToInt16(Request.QueryString["idt"]);
     
        if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode )
        {
            GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
     
            TSEntities db = new TSEntities();
            Ticket ticket = new Ticket();
     
            RadComboBox rcbTip = (RadComboBox)userControl.FindControl("rcbTip");
            rcbTip.Items.Add(new RadComboBoxItem("Incident"));
            rcbTip.Items.Add(new RadComboBoxItem("Reklamacija"));
            rcbTip.Items.Add(new RadComboBoxItem("Nova funkcionalnost"));
            rcbTip.DataBind();
     
            RadComboBox rcbStatus = (RadComboBox)userControl.FindControl("rcbStatus");
            rcbStatus.Items.Add(new RadComboBoxItem("New"));
            rcbStatus.Items.Add(new RadComboBoxItem("U radu"));
            rcbStatus.Items.Add(new RadComboBoxItem("On hold"));
            rcbStatus.Items.Add(new RadComboBoxItem("Pending"));
            rcbStatus.Items.Add(new RadComboBoxItem("Scheduled"));
            rcbStatus.Items.Add(new RadComboBoxItem("Canceled"));
            rcbStatus.Items.Add(new RadComboBoxItem("Completed"));
            rcbStatus.DataBind();
     
            RadComboBox rcbVrstaPrijave = (RadComboBox)userControl.FindControl("rcbVrstaPrijave");
            rcbVrstaPrijave.Items.Add(new RadComboBoxItem("Usmeno"));
            rcbVrstaPrijave.Items.Add(new RadComboBoxItem("Telefon"));
            rcbVrstaPrijave.Items.Add(new RadComboBoxItem("E-mail"));
            rcbVrstaPrijave.Items.Add(new RadComboBoxItem("Web"));
            rcbVrstaPrijave.DataBind();
     
            RadComboBox rcbPrioritet = (RadComboBox)userControl.FindControl("rcbPrioritet");
            rcbPrioritet.Items.Add(new RadComboBoxItem("Low"));
            rcbPrioritet.Items.Add(new RadComboBoxItem("Normal"));
            rcbPrioritet.Items.Add(new RadComboBoxItem("High"));
            rcbPrioritet.Items.Add(new RadComboBoxItem("Odmah"));
            rcbPrioritet.DataBind();
     
            CheckBox chkNadreden = (CheckBox)userControl.FindControl("chkNadreden");
     
            RadComboBox rcbNadredeniTicket = (RadComboBox)userControl.FindControl("rcbNadredeniTicket");
            var nadredenTicketList = (from t in db.Ticket
                                        select t).ToList();
     
            rcbNadredeniTicket.DataSource = from t in nadredenTicketList
                                            where t.idFirma == idFirma && t.Zatvoren == false
                                            select new { t.idTicket, OpisTicketa = t.idTicket + " - " + t.Opis };
     
            rcbNadredeniTicket.Items.Add(new RadComboBoxItem("Bez nadređenog ticketa", "0"));
            rcbNadredeniTicket.DataBind();
     
            RadComboBox rcbAsset = (RadComboBox)userControl.FindControl("rcbAsset");
            var ticketAssetList = (from a in db.Asset
                                   where a.idFirma == idFirma
                                   select a).ToList();
     
            if (ticketAssetList.Count > 0)
            {
                var assets = from a in db.Asset
                             where a.idFirma == idFirma
                             select new { a.idAsset, a.Naziv };
     
                rcbAsset.DataSource = assets;
                rcbAsset.DataTextField = "Naziv";
                rcbAsset.DataValueField = "idAsset";
                rcbAsset.Text = "'<%# DataBinder.Eval(Container, 'DataItem.Naziv') %>'";
                rcbAsset.DataBind();
            }                            
             
             
            if (!e.Item.OwnerTableView.IsItemInserted)
            {
                int idTicket = Convert.ToInt32(editFormItem.GetDataKeyValue("idTicket"));
                ticket = db.Ticket.SingleOrDefault(t => t.idTicket == idTicket);
     
                string tip = ticket.Tip;
                rcbTip.Items.FindItemByText(tip).Selected = true;
     
                string status = ticket.Status;
                rcbStatus.Items.FindItemByText(status).Selected = true;
     
                string vrstaPrijave = ticket.VrstaPrijave;
                rcbVrstaPrijave.Items.FindItemByText(vrstaPrijave).Selected = true;
     
                string prioritet = ticket.Prioritet;
                rcbPrioritet.Items.FindItemByText(prioritet).Selected = true;
                 
            }              
        }
    }

    The problem occurs in the line with RadComboBox rcbAsset. The problem is a bit urgent so any help would be appreciated!
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 Aug 2011 Link to this post

    Hello Ignjat,

    After examining your code you have given the ID of RadComboBox in UserControl as rcbAsseti.
    C#:
    RadComboBox rcbAsset = (RadComboBox)userControl.FindControl("rcbAsseti");
    So try accessing the RaDComboBox with same ID

    Thanks,
    Shinu.
  3. Ignjat
    Ignjat avatar
    13 posts
    Member since:
    Aug 2011

    Posted 31 Aug 2011 Link to this post

    Oh... Now I feel bad for panicking (and for losing almost 15 hours on this). Thank you!

    As they say in my country: "Two heads are always better than just one!"
Back to Top