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

RadComboBox RequiredFieldValidator within RadGrid EditUserControl

5 Answers 150 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Hrvach
Top achievements
Rank 1
Hrvach asked on 25 Oct 2011, 10:02 AM
Hello!

I've seen couple of examples on how to validate RadComboBox with RequiredFieldValidator, but I can't get it working. In my scenario I have RadGrid with EditUserControl which has couple of RadComboBoxes I tried to validate, but without any success. What happens is that on a button click code is executed, but without any validation.

This is the code example of a UserControl:

<div class="ucWrapper">
    <div class="ucLeft">
        <table>
            <tr>
                <td>
                    Vrsta prijave:
                </td>
                <td>
                    <telerik:RadComboBox ID="rcbVrstaPrijave" Text='<%# DataBinder.Eval(Container, "DataItem.VrstaPrijave") %>'
                        runat="server" Skin="Hay">
                    </telerik:RadComboBox>
                    <asp:RequiredFieldValidator ID="rfv1" ControlToValidate="rcbVrstaPrijave"
                        runat="server" ErrorMessage="*" InitialValue="0"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Tip:
                </td>
                <td>
                    <telerik:RadComboBox ID="rcbTip" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Tip") %>'
                        Skin="Hay">
                    </telerik:RadComboBox>
                                   
                    Status:               
                    <telerik:RadComboBox ID="rcbStatus" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'
                        Skin="Hay">
                    </telerik:RadComboBox>
                    <asp:RequiredFieldValidator runat="server" ID="rfv2" ValidationGroup="Group1" ControlToValidate="rcbTip"
                        Display="dynamic" ErrorMessage="*" />
                </td>
            </tr>
            <tr>
                <td>
                    Opis:
                </td>
                <td>
                    <asp:TextBox ID="txtOpis" runat="server" TextMode="MultiLine" Width="455px" Height="200px"
                        Text='<%# DataBinder.Eval(Container, "DataItem.Opis") %>' Font-Names="Calibri"
                        Font-Size="10pt"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnUpdate" Text="Spremi" runat="server" CommandName="Update" ValidationGroup="Group1" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'>
                    </asp:Button>
                    <asp:Button ID="btnInsert" Text="Spremi" runat="server" ValidationGroup="Group1" CommandName="PerformInsert"
                        Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:Button>
                </td>

Any help would be appreciated.

Thank you,

Hrvoje

5 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 25 Oct 2011, 01:49 PM
Hello Hrvoje,

The two things I notice in your code is that the first RequiredFieldValidator doesn't have the ValidationGroup set, so it's validation will never be raised during the button click. The second RequiredFieldValidator doesn't have an InitialValue set, so it doesn't have something to compare it with, unless the first item in your drop-down doesn't have the Value property specified.

I also have one question. Why are you binding to the RadComboBox's Text property and not the Value property, when you don't have AllowCustomText specified on any of your RadComboBox controls?

I hope that helps.
0
Hrvach
Top achievements
Rank 1
answered on 25 Oct 2011, 10:25 PM
I must have copied the wrong version of code, that's the only reason for not having InitialValue and ValidationGroup parameters set. What bothers me is that RequiredFieldValidators behave like they don't exist - code executes like they don't even exist. I've even tried with the CustomValidator control (even though I'd rather do it other way), but it was helpful only for the first RadComboBox and the same thing happened as now with RequiredFieldValidators - application execution continued.

Regarding your second question. To be honest, I'm not sure why. I tried to recreate an example I've found somewhere on the web and since it was working I had no reason to change it.

Thank you for your reply! I appreciate it, but, unfortunately, it didn't help me.
0
Kevin
Top achievements
Rank 2
answered on 26 Oct 2011, 01:30 PM
Hello Hrvoje,

How are you loading the items in the RadComboBox? Does the first item in the list, I'm assuming it must be a 'Select' item, does it have it's value set to 0? Could you show us what your code looks like with relation to how you load the items?
0
Hrvach
Top achievements
Rank 1
answered on 26 Oct 2011, 02:22 PM
Hello Kevin,

here is example of binding data to the RadComboBoxes, this example is for a form other than the one in my first post, but as the concept is exactly the same it shouldn't matter. So, data is binded via ItemDataBound function of the RadGrid. First item inside (some) RadComboBoxes has always the value of "0" because it shows something like "Choose an item from the list".

protected void gvDaily_ItemDataBound(object sender, GridItemEventArgs e)
{
    int idAuthenticatedManager = Convert.ToInt32(Session["authenticatedUI"]);
 
    if ((e.Item is GridEditFormItem) && e.Item.IsInEditMode)
    {
        GridEditFormItem editFormItem = (GridEditFormItem)e.Item;
        UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
 
        RadComboBox rcbTicket = (RadComboBox)userControl.FindControl("rcbTicket");
        RadComboBox rcbManager = (RadComboBox)userControl.FindControl("rcbManager");
        RadComboBox rcbStatus = (RadComboBox)userControl.FindControl("rcbStatus");
        RadDatePicker txtDatum = (RadDatePicker)userControl.FindControl("txtDatum");
        Button btnUpdate = (Button)userControl.FindControl("btnUpdate");
        Button btnInsert = (Button)userControl.FindControl("btnInsert");
        CheckBox chkDolazak = (CheckBox)userControl.FindControl("chkDolazak");
 
        txtDatum.SelectedDate = DateTime.Now;
 
        var ticketList = (from t in db.Ticket
                          where t.idManager == idAuthenticatedManager && t.Zatvoren == false || t.idManager == null && t.Zatvoren == false
                          select t).ToList();
 
        if (ticketList.Count > 0)
        {
            rcbTicket.DataSource = from t in ticketList                                      
                                   orderby t.Firma.Naziv, t.DatumPrijave, t.Zatvoren
                                   select new { t.idTicket, OpisTicketa = t.Firma.Naziv + " - " + t.idTicket + " - " + t.Opis };
            rcbTicket.DataTextField = "OpisTicketa";
            rcbTicket.DataValueField = "idTicket";
            rcbTicket.Text= "'<%# DataBinder.Eval(Container, 'DataItem.idTicket') %>'";
            rcbTicket.Items.Add(new RadComboBoxItem("Odaberi ticket", "0"));
        }
        else
        {
            rcbTicket.Items.Add(new RadComboBoxItem("Ne postoje ticketi", "0"));
            btnUpdate.Visible = false;
            btnInsert.Visible = false;
        }
        rcbTicket.DataBind();
 
        var managerList = (from m in db.Kontakt
                           select m).ToList();
 
        if (managerList.Count > 0)
        {
            rcbManager.DataSource = from m in managerList
                                    where m.Firma.Naziv == "IDE3"
                                    orderby m.Prezime, m.Ime
                                    select new { m.idKontakt, Naziv = m.Ime + " " + m.Prezime };
            rcbManager.DataTextField = "Naziv";
            rcbManager.DataValueField = "idKontakt";
            rcbManager.Text = "'<%# DataBinder.Eval(Container, 'DataItem.idKontakt') %>'";
 
            rcbManager.Items.Add(new RadComboBoxItem("Odaberi managera", "0"));
        }
        rcbManager.DataBind();
        rcbManager.FindItemByValue(idAuthenticatedManager.ToString()).Selected = true;
 
        if (!e.Item.OwnerTableView.IsItemInserted)
        {
            Daily daily = new Daily();
 
            int idDaily = Convert.ToInt32(editFormItem.GetDataKeyValue("idDaily"));
            daily = db.Daily.SingleOrDefault(d => d.idDaily == idDaily);
 
            int idManager = daily.idManager;
            rcbManager.Items.FindItemByValue(idManager.ToString()).Selected = true;
 
            int idTicket = daily.idTicket;
            rcbTicket.Items.FindItemByValue(idTicket.ToString()).Selected = true;
 
            txtDatum.SelectedDate = daily.Datum;
 
            Ticket ticket = new Ticket();
 
            rcbStatus.Items.Clear();
            rcbStatus.Items.Add(new RadComboBoxItem("New"));
            rcbStatus.Items.Add(new RadComboBoxItem("U radu"));
            rcbStatus.Items.Add(new RadComboBoxItem("Testiranje"));
            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();
 
            ticket = db.Ticket.SingleOrDefault(t => t.idTicket == idTicket);
            rcbStatus.Items.FindItemByText(ticket.Status).Selected = true;
 
            TextBox txtBiljeske = (TextBox)userControl.FindControl("txtBiljeske");
            txtBiljeske.Text = ticket.Biljeske;
 
            if (daily.Dolazak == true)
            {
                chkDolazak.Checked = true;
            }
            else
            {
                chkDolazak.Checked = false;
            }
        }
    }
}


Thank you for your efforts, I appreciate it!
0
Accepted
Dimitar Terziev
Telerik team
answered on 28 Oct 2011, 11:24 AM
Hello Hrvoje,

Based on the mark-up provided the experienced behavior is the expected one.

First the RequiredFieldValidators will never fire in your case, due to the fact that this validators check whether there is a text entered in the input of the RadComboBoxes. In your case you will always have some text and thus the validation will pass every time.

In case you want to ensure that an existing item is selected, then you should use CustomValidators and in the client-side validation function you will check whether there is a selected item or not.

Best wishes,
Dimitar Terziev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
ComboBox
Asked by
Hrvach
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Hrvach
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Share this question
or