Retrieving RadComboBox.SelectedValue during PerformInsertCommandName

8 posts, 0 answers
  1. Luigi Gaeta
    Luigi Gaeta avatar
    16 posts
    Member since:
    Jul 2005

    Posted 03 Dec 2009 Link to this post

    Hi,
    I'm trying to retrieve the SelectedValue for a RadComboBox putted in the InsertItemTemplate.
    The code used as follow:

        protected void RadListPermessi_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.CancelCommandName)
            {
                RadListPermessi.InsertItemPosition = RadListViewInsertItemPosition.None;
                RadListPermessi.FindControl("btnAddNewRecord").Visible = true;
            }
            else if (e.CommandName == RadListView.UpdateCommandName)
            {
                //Perform the update
                string perDebug = "Stop";
            }
            else if (e.CommandName == RadListView.PerformInsertCommandName)
            {
                //Va ad aggiungere un nuovo item alla ListaPermessiForms
                PermessoForm permessoForm = new PermessoForm();
                RadComboBox comboNomeForm = (RadComboBox)e.ListViewItem.FindControl("radComboNomeFormInsert");
                if (comboNomeForm != null)
                {
                    permessoForm.IdForm = int.Parse(comboNomeForm.SelectedValue);
                }
    ... omissis ...


    I'm able to see the "Text" of the selected item, but the SelectedValue is an empty string (instead of the ID value that I've put in the value field of the radCombo).
    Thanks for any help.
    Luigi
  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 03 Dec 2009 Link to this post

    Hello Luigi,

    Is it possible that you use load-on-demand for the combobox that resides in the insert template of RadListView? If so, try to extract the SelectedItem.Value property or the Value property of the combobox directly to see whether this produces the desired result. 

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Luigi Gaeta
    Luigi Gaeta avatar
    16 posts
    Member since:
    Jul 2005

    Posted 03 Dec 2009 Link to this post

    Thanks for your quick replay.
    My scenario as follow, I put the RadListView in a UserControl that is hosted in an aspx. I use the declarative mode (not load on demand) for the UserControl and for the control inside this.
    To fill the RadComboBox that is present in the InsertItemTemplate I use the ItemCreated event.
    The code for this event is:

        protected void RadListPermessi_ItemCreated(object sender, RadListViewItemEventArgs e)
        {
            if (e.Item is RadListViewEditableItem && e.Item.IsInEditMode)
            {
                var comboNomeForm = e.Item.FindControl("radComboNomeFormInsert");
                RiempiComboNomiForms((RadComboBox)comboNomeForm);
            }
            return;
        }

     

        private void RiempiComboNomiForms(RadComboBox comboNomeForm)
        {
            if (comboNomeForm != null)
            {   //Riempie con i nomi dei form
                ListaForms listaForms = new ListaForms(string.Empty, "frmTitolo", TipoDbPersonalizzato, ConnectionStringDbPersonalizzato);
                foreach (Form form in listaForms)
                {
                    comboNomeForm.Items.Add(new RadComboBoxItem(form.Nome, form.ID.ToString()));
                }
                comboNomeForm.SelectedIndex = 0;
            }
            return;
        }



    while for the design view I've used:

    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

    <telerik:RadListView runat="server" ID="RadListPermessi" AllowPaging="false" ItemPlaceholderID="itemPlaceholder"
        OnItemCommand="RadListPermessi_ItemCommand"
        OnItemCreated="RadListPermessi_ItemCreated"
        OnNeedDataSource="RadListPermessi_NeedDataSource"
        OnItemDataBound="RadListPermessi_ItemDataBound"
    >
        <LayoutTemplate>
            <div style="margin-bottom:10px;">
                <asp:Button runat="server" ID="btnAddNewRecord" Text="Aggiungi Permesso"
                    CommandName="InitInsert" OnClick="AddNew_Click" />
            </div>
            <table style="width: 270px; background-color: #D9DFDF;">
                <tr>
                    <th><!--Lasciare vuoto, serve per i bottoni di edit e update--></th>
                    <th id="Th1" runat="server">Sezione</th>
                    <th id="Th2" runat="server">Visibilità</th>
                    <th id="Th3" runat="server">Lettura</th>
                    <th id="Th4" runat="server">Modifica</th>
                    <th id="Th5" runat="server">Elimina</th>
                    <th id="Th6" runat="server">Crea</th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
        </LayoutTemplate>
        <EmptyDataTemplate>
            <div style="margin-bottom:10px;">
                <asp:Button runat="server" ID="btnAddNewRecord" Text="Aggiungi Permesso"
                    CommandName="InitInsert" OnClick="AddNew_Click" />
            </div>
            Per creare un nuovo permesso, fare click sul bottone "Aggiungi Permesso"
        </EmptyDataTemplate>
        <EditItemTemplate>
            <tr id="Tr2" runat="server" bgcolor="#45474A">
                <td style="width: 100px;">
                    <asp:ImageButton runat="server" ID="UpdateButton" CommandName="Update" ImageUrl="~/images/grid/Update.gif" />
                    <asp:ImageButton ID="CancelButton" runat="Server" Text="Cancel" CommandName="Cancel" ImageUrl="~/images/grid/Cancel.gif" />
                </td>
                <td>
                    <telerik:RadComboBox runat="server" ID="radComboNomeForm"></telerik:RadComboBox>
                </td>
                <td valign="top">
                    <telerik:RadComboBox runat="server" ID="radComboVisibilita"></telerik:RadComboBox>
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkLettura" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkModifica" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkEliminazione" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkCreazione" />
                </td>
            </tr>
        </EditItemTemplate>
        <InsertItemTemplate>
            <tr id="Tr2" runat="server" bgcolor="#45474A">
                <td style="width: 100px;">
                    <asp:ImageButton runat="server" ID="InsertButton" CommandName="PerformInsert" ImageUrl="~/images/grid/Update.gif" />
                    <asp:ImageButton ID="CancelButton" runat="Server" Text="Cancel" CommandName="Cancel" ImageUrl="~/images/grid/Cancel.gif" />
                </td>
                <td>
                    <telerik:RadComboBox runat="server" ID="radComboNomeFormInsert"></telerik:RadComboBox>
                </td>
                <td valign="top">
                    <telerik:RadComboBox runat="server" ID="radComboVisibilitaInsert"></telerik:RadComboBox>
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkLettura" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkModifica" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkEliminazione" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkCreazione" />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr id="Tr2" runat="server" bgcolor="#99ccff">
                <td>
                    <asp:ImageButton runat="server" ID="EditButton" CommandName="Edit" ImageUrl="~/images/grid/Edit.gif" />
                </td>
                <td>
                    <asp:Label ID="lblNomeForm" runat="Server" />
                </td>
                <td valign="top">
                    <asp:Label ID="lblVisibilita" runat="Server" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkDisabledLettura" Enabled="false" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkDisabledModifica" Enabled="false" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkDisabledEliminazione" Enabled="false" />
                </td>
                <td valign="top">
                    <asp:CheckBox runat="server" ID="chkDisabledCreazione" Enabled="false" />
                </td>
            </tr>
        </ItemTemplate>
    </telerik:RadListView>

     

    I confirm that only "Text" field contains the correct value for the selected item, while SelectedItem.Value and SelectedValue are empty strings.

  5. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 03 Dec 2009 Link to this post

    Hello Luigi,

    From the code snippets you provided I see that you instantiate the combobox editor items for each edited/inserted item on ItemCreated and set the SelectedIndex of the combobox to 0 (I assume this is done in par with your custom logic).

    However, note that the ItemCreated event is raised prior to the actual insert operation as specified in this article, hence the item in the combo is reset to the first before the insert logic takes place. In case the first combobox item has an empty value, you will get the result you specified.

    To avoid that, consider populating the items inside the ItemDataBound server event of the grid.(using the same conditional statement). You may also remove the return calls from the methods as they are not necessary (and the SelectedIndex setting if not mandatory).

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Luigi Gaeta
    Luigi Gaeta avatar
    16 posts
    Member since:
    Jul 2005

    Posted 03 Dec 2009 Link to this post

    In ItemDataBound the item is never in Edit mode and is of type "RadListViewDataItem".
    Changing the code that was in _ItemCreated for this new type of item and putting in _ItemDataBound, I'm not able to retrieve the control at all, with this code:

        protected void RadListPermessi_ItemDataBound(object sender, RadListViewItemEventArgs e)
        {
            if (e.Item is RadListViewDataItem)
            {
                var comboNomeForm = e.Item.FindControl("radComboNomeForm");
                RiempiComboNomiForms((RadComboBox)comboNomeForm);
                RiempiComboVisibilita((RadComboBox)e.Item.FindControl("radComboVisibilita"));
            }
            return;
        }


    the "comboNomeForm" is always null.

    For the value retrieving I repeat that the "Text" property of the combo give me the correct value, the problem is for the SelectedValue (and SelectedItem.Text and SelectedItem.Value) property: all to empty string!

  7. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 03 Dec 2009 Link to this post

    Hello Luigi,

    I think that the following modification should do the trick:

    protected void RadListPermessi_ItemDataBound(object sender, RadListViewItemEventArgs e)
    {
        if (e.Item is RadListViewInsertItem && (RadListView1.InsertItemPosition != RadListViewInsertItemPosition.None))
        {
            var comboNomeForm = e.Item.FindControl("radComboNomeForm");
            RiempiComboNomiForms((RadComboBox)comboNomeForm);
            RiempiComboVisibilita((RadComboBox)e.Item.FindControl("radComboVisibilita"));
        }
        return;
    }

    I suppose that tour previous implementation would return proper text value in case you do not select a different item from the combobox editor.

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Luigi Gaeta
    Luigi Gaeta avatar
    16 posts
    Member since:
    Jul 2005

    Posted 03 Dec 2009 Link to this post

    No, the only type of the e.Item that I reach in the _ItemDataBound event is "RadListViewDataItem".
    Perhaps the problem is in the AddNew_Record button? The code for this purpose is:

    protected void AddNew_Click(object sender, EventArgs e)
        {
            RadListPermessi.InsertItemPosition = RadListViewInsertItemPosition.FirstItem;
            (sender as Button).Visible = false;
            PermessiForms.Insert(0, new PermessoForm());    //Inserisce il nuovo item in testa
            RadListPermessi.DataSource = PermessiForms;     //Senza riassegnare al DataSource non succede nulla (il NeedDataSource non scatta)
            RadListPermessi.DataBind();
            return;
        }

    where "PermessiForms" is an IList custom object.
    If I delete the last 3 rows of code, nothing happen in my RadListView!
    Help please, I'm loosing so much time...
  9. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 03 Dec 2009 Link to this post

    Hello Luigi,

    Please excuse me if my previous reply was a bit misleading - it appeared that the ItemDataBound event will not be fired for the insert item of the listview because it is not bound to data. Hence my suggestion would be to remove merely the SelectedIndex reset from your previous code inside the RiempiComboNomiForms method, i.e.:

    private void RiempiComboNomiForms(RadComboBox comboNomeForm)
    {
        if (comboNomeForm != null)
        {   //Riempie con i nomi dei form
            ListaForms listaForms = new ListaForms(string.Empty, "frmTitolo", TipoDbPersonalizzato, ConnectionStringDbPersonalizzato);
            foreach (Form form in listaForms)
            {
                comboNomeForm.Items.Add(new RadComboBoxItem(form.Nome, form.ID.ToString()));
            }
            //comboNomeForm.SelectedIndex = 0;
        }
        //return;
    }

    Thus the first item will be preselected on initial insert and the selected value should be recovered from the viewstate when the ItemCreated event is raised before the insert operation is processed.

    If this does not help, I suggest you isolate a simplified working version of your project and send it enclosed to a regular support ticket. I will test it locally and will advice you further.

    Kind regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017