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

ComboBox and DataValueField

16 Answers 631 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Dariusz Tomoń
Top achievements
Rank 1
Dariusz Tomoń asked on 31 Jul 2008, 01:12 PM

Hello,

I have ComboBox:

<telerik:RadComboBox ID="RadComboBox_Branze" runat="server" DataTextField="NAZWA_BRANZA" DataValueField="idTB_BRANZA" EnableLoadOnDemand="True" OnItemsRequested="RadComboBox_Branze_ItemsRequested" Width="300px" ><CollapseAnimation Duration="200" Type="OutQuint" /></telerik:RadComboBox>

In properties of RadComboBox I set:

DataTextField="businesslinename"
DataValueField="id_businessline"

where businesslinename is string value and id_businessline is int value, both retrieved from database

C# method to fill combo:

protected

void RadComboBox_Branze_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)

{

if (e.Text.Length > 3)

{

RadComboBox combo = (RadComboBox)o;

combo.Items.Clear();

string sql = "select * from tb_branza where nazwa_branza like '%" + e.Text + "%' AND idTB_JEZYK=1";

//SqlDataSource1

 

SqlDataAdapter adapter = new SqlDataAdapter(sql, SqlDataSource1.ConnectionString.ToString());

DataTable data = new DataTable();

adapter.Fill(data);

try

 

{

int itemsPerRequest = 100;

int itemOffset = e.NumberOfItems;

int endOffset = itemOffset + itemsPerRequest;

if (endOffset > data.Rows.Count)

{

endOffset = data.Rows.Count;

}

if (endOffset == data.Rows.Count)

{

e.EndOfItems =

true;

}

else

 

{

e.EndOfItems =

false;

}

for (int i = itemOffset; i < endOffset; i++)

{

RadComboBox_Branze.Items.Add(

new RadComboBoxItem(data.Rows[i]["nazwa_branza"].ToString(), data.Rows[i]["nazwa_branza"].ToString()));

}

if (data.Rows.Count > 0)

{

e.Message =

String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString());

}

else

 

{

e.Message =

"No matches";

}

}

catch

{

e.Message =

"No matches";

}

}

}

I have got Button to submit method:


protected void btn_DodajBranze_Click(object sender, EventArgs e)

{

TextBox1.Text = RadComboBox_Branze.SelectedValue;

//DodajBranze(id_zamowienie_dns);

}

The problem is that I get DataTextField="businesslinename" instead DataValueField="id_businessline" into my TextBox1. How to retrieve a value from RadComboBox instead of text.

Best Regards

Dariusz TomoĊ„

16 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 31 Jul 2008, 02:44 PM
Hi Dariusz TomoĊ„,

I have reviewed your code and notice the following things.

1.In your aspx definition you already have set the DataTextField and the DataValueField properties - DataTextField="NAZWA_BRANZA" DataValueField="idTB_BRANZA"


2.When you load items on demand you add them with text and value form the NAZWA_BRANZA again
new RadComboBoxItem(data.Rows[i]["nazwa_branza"].ToString(), data.Rows[i]["nazwa_branza"].ToString()));

Could you explain more clear how do you set DataTextField="businesslinename"
DataValueField="id_businessline" and which fields you want to use - businesslinename and id_businessline or
NAZWA_BRANZA and idTB_BRANZA?

However I suggest you change the line above like follows:
new RadComboBoxItem(data.Rows[i]["nazwa_branza"].ToString(), data.Rows[i]["idTB_BRANZA"].ToString()));
and let us known how this goes.

Hope this helps.

Regards, Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Dariusz Tomoń
Top achievements
Rank 1
answered on 01 Aug 2008, 02:46 PM
Hi,

Even though my explanation was awkward you still get by to help me.
The clue was the line:

However I suggest you change the line above like follows:
new RadComboBoxItem(data.Rows[i]["nazwa_branza"].ToString(), data.Rows[i]["idTB_BRANZA"].ToString()));
and let us known how this goes.

Thank you.

Darek
0
Jesús
Top achievements
Rank 1
answered on 27 Nov 2009, 11:43 AM
I have the same problem, my text field has descriptive texts and the value field has the codes, the real values we store in the BBDD.

When submitting, we gets the descriptive texts instead of the codes.
0
Simon
Telerik team
answered on 27 Nov 2009, 05:09 PM
Hi El senyor dels Bertins,

Please make sure that the DataTextField and DataValueField properties are set to the respective fields and are not swapped.

In any case, please paste here the definition of your RadComboBox or the code that binds it the data source.

Regards,
Simon
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.
0
Jesús
Top achievements
Rank 1
answered on 30 Nov 2009, 05:19 PM
I'm filling the RadComboBox with a for like this:

        foreach (DataRow dr in drArr) 
        { 
            rcb.Items.Add(new RadComboBoxItem(dr["textField"].ToString(), dr["valueField"].ToString())); 
        } 
 

Or, if the same, we translated the commands for the default DropDownList to RadComboBox... and the submit behaviour is quite different.


0
Simon
Telerik team
answered on 01 Dec 2009, 03:32 PM
Hi El senyor dels Bertins,

Can you please also paste here the definition of the RadComboBox and make sure that the correct values exist in the valueField column?

Best wishes,
Simon
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.
0
Jesús
Top achievements
Rank 1
answered on 01 Dec 2009, 04:17 PM
I paste the method I use to create RadComboBox. It's part of a code used to create maintenance and filter screens dynamically.

private RadComboBox CrearCampoCombo(string id, bool dinamico, string codigoCombo, bool required, object defValue) 
    { 
        RadComboBox rcb = new RadComboBox(); 
        rcb.ID = id; 
        rcb.Width = Unit.Pixel(150); 
        if (!required) 
        { 
            rcb.Items.Add(new RadComboBoxItem("""")); 
        } 
        DataRow[] drArr = wc.GetDropDownList(dinamico, codigoCombo, idioma, Page.User.Identity.Name); 
        foreach (DataRow dr in drArr) 
        { 
            rcb.Items.Add(new RadComboBoxItem(dr["texto"].ToString(), dr["valor"].ToString())); 
            if (defValue != null && defValue.Equals(dr["valor"])) 
            { 
                rcb.Items[rcb.Items.Count - 1].Selected = true
            } 
        } 
        return rcb; 
    } 

where dr["texto"] contains the text the user can read and dr["valor"] contains the values to send to the server if selected.


0
Simon
Telerik team
answered on 01 Dec 2009, 05:00 PM
Hello El senyor dels Bertins,

Please verify exactly what data is coming from the data base in the dr["texto"] and dr["valor"] fields. Are the two fields' values different or the same?

Kind regards,
Simon
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.
0
Jesús
Top achievements
Rank 1
answered on 01 Dec 2009, 05:24 PM
They are different... One example:
texto: "Following"; value: "FOL"
texto: "Preceeding"; value: "PRE"
texto: "Modified following"; value: "MFO"
texo: "Modified preceeding"; value: "MPR"
0
Simon
Telerik team
answered on 02 Dec 2009, 09:04 AM
Hello El senyor dels Bertins,

Thank you for verifying this.

This RadComboBox functionality is working everywhere in our demos in the expected way. This means that there must be something else on your side which is causing the issue.

Can you please paste here the definition of the RCB and all of the code referencing it in the client code and in the code-behind? (You can use the Code Snippet tool of the editor to paste all of the files' content. You can also remove the unrelated code.)

Additionally, please let me know which version of Telerik.Web.UI are you using.

Regards,
Simon
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.
0
Jesús
Top achievements
Rank 1
answered on 02 Dec 2009, 09:21 AM
We are using the 2009 Q1 version.

Here the code referencing the sample combo I showed before.

Sys.Application.add_init(function() { 
    $create(Telerik.Web.UI.RadComboBox, {"_dropDownWidth":0,"_height":0,"_skin":"Blues","_text":"Modified Following","_uniqueId":"DefMaint$convFechas","_value":"MFO","_virtualScroll":false,"clientStateFieldID":"DefMaint_convFechas_ClientState","collapseAnimation":"{\"type\":12,\"duration\":200}","enabled":false,"itemData":[{"value":"FOL"},{"value":"MFO","selected":true},{"value":"MPR"},{"value":"PRE"}],"selectedIndex":1}, null, null, $get("DefMaint_convFechas")); 
}); 
 


<td style="text-align:Left;"
  <span id="DefMaint_convFechas_label">Conv. fechas</span> 
</td> 
<td style="text-align:Left;"
<div id="DefMaint_convFechas" class="RadComboBox RadComboBox_Blues " style="width:160px;display:inline;zoom:1;"
  <table class="rcbDisabled" cellpadding="0" cellspacing="0" summary="combobox" border="0" style="border-width:0;table-layout:fixed;border-collapse:collapse;width:100%"
    <tr> 
      <td class="rcbInputCell rcbInputCellLeft" style="margin-top:-1px;margin-bottom:-1px;width:100%;"><input value="Modified Following" class="rcbInput" id="DefMaint_convFechas_Input" title="Conv. fechas" name="DefMaint$convFechas" style="display:block;text-decoration:;" type="text" disabled="disabled" readonly="readonly"></input> 
      </td> 
      <td class="rcbArrowCell rcbArrowCellRight" style="margin-top:-1px;margin-bottom:-1px;"><id="DefMaint_convFechas_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a> 
      </td> 
    </tr> 
  </table> 
  <div class="rcbSlide" style="z-index:6000;"
    <div id="DefMaint_convFechas_DropDown" class="RadComboBoxDropDown_Blues" style="float:left;display:none;"
      <div class="rcbScroll rcbWidth" style="width:100%;"
        <ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;"
          <li class="rcbItem ">Following</li> 
          <li class="rcbItem ">Modified Following</li> 
          <li class="rcbItem ">Modified Preceeding</li> 
          <li class="rcbItem ">Preceeding</li> 
        </ul> 
      </div> 
    </div> 
  </div> 
  <input id="DefMaint_convFechas_ClientState" name="DefMaint_convFechas_ClientState" type="hidden" /> 
</div> 
</td> 

Hope this would help.


0
Simon
Telerik team
answered on 09 Dec 2009, 01:04 PM
Hi El senyor dels Bertins,

Thank you for the code.

Could you also paste here the code (HTML markup, JavaScript and C#) from the aspx and aspx.cs files which uses the RadComboBox .

Sincerely yours,
Simon
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.
0
Jesús
Top achievements
Rank 1
answered on 25 Feb 2011, 10:33 AM
Now I have time again and I can try to resolve this.

At current we use the Q1 of 2010, and I'm still having the same problem, when submitting RadComboBox sends the text field, not the value field.
0
Simon
Telerik team
answered on 01 Mar 2011, 02:03 PM
Hi El senyor dels Bertins,

It is normal to see in the submitted form fields the RadComboBox' text and not its value. The latter is submitted through the Client State field of RadComboBox. Then the control parses this field and extracts the Value so that it is later accessible through the SelectedValue property.

Can you please restate what exactly is the problem you are experiencing? Can you also open a support ticket and send us a sample project, which shows the problem (and mention this forum thread as well)?

We will respond to both places once we find the solution.

Thank you in advance for your co-operation and understanding.

All the best,
Simon
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Jesús
Top achievements
Rank 1
answered on 03 Mar 2011, 01:24 PM
I've opened a support ticket.

Thx.
0
Kalina
Telerik team
answered on 08 Mar 2011, 12:26 PM
Hi El senyor dels Bertins,

The RadComboBox and ASP.NET DropDownList are absolutely different controls - they differ by design, implementation and behaviour.
The DropDownList is not an AJAX control, does not have client-side functionality and it renders as a "<select>" - that is why you are able to get the selected value.
The RadComboBox renders as an "<input>" so it is normal to obtain the text entered in this input via Request object.
That is why you have to use the SelectedValue of the RadComboBox in order to get the selected value.

Regards,
Kalina
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
ComboBox
Asked by
Dariusz Tomoń
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Dariusz Tomoń
Top achievements
Rank 1
Jesús
Top achievements
Rank 1
Simon
Telerik team
Kalina
Telerik team
Share this question
or