PDA

View Full Version : Problem setting SelectedItem.Value in combobox v2.0



Oliver
Apr 02, 2012, 1:18 PM
Hello!

After convert an application to version 2.0 I'm having problem to set a particular value to a combobox. When I try to set a value I received a message about Object Instance null exception (System.NullReferenceException).

My combobox code:



<ext:ComboBox ID="dbAtivo" runat="server" Editable="false" Height="20px"
FieldLabel="Ativo" Width="300px" ReadOnly="true" >
<Items>
<ext:ListItem Text="Yes" Value="Y" />
<ext:ListItem Text="No" Value="N" />
</Items>
</ext:ComboBox>


Code to set a value from database:



dbAtivo.SelectedItem.Value = drSql.GetString(4);


Thanks in advance!

Daniil
Apr 03, 2012, 4:04 PM
Hi,

Please use:

ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "some value" });

Oliver
Apr 04, 2012, 5:27 PM
Hi,

Please use:

ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "some value" });

Hi Daniil,

Thanks for help, it works. I tried another code using Combobox1.SetValue(some value) and works too.

You can mark as closed.

Thanks.

Oliver

hemantpatil
Aug 23, 2013, 9:00 AM
Hi,

Please use:

ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "some value" });

Hi Daniil,

i m facing the same issue. what i m trying to do is, i have a combobox displaying list of say employees, i've set Code as ValueField and Name as DisplayField for the Combobox. i m using asp.net with VB so i m not able try your above stated solution. (don't know how to use it in VB).
i store Employee Code to the database and not Name. and after retrieving the data from database i want to set the name related to code in database as selected item.

i've also tried using ComboBox1.SetValue, but it sets code as text and not selected value.

thanks in advance.

blueworld
Aug 23, 2013, 9:55 AM
Hi Daniil,

i m facing the same issue. what i m trying to do is, i have a combobox displaying list of say employees, i've set Code as ValueField and Name as DisplayField for the Combobox. i m using asp.net with VB so i m not able try your above stated solution. (don't know how to use it in VB).
i store Employee Code to the database and not Name. and after retrieving the data from database i want to set the name related to code in database as selected item.

i've also tried using ComboBox1.SetValue, but it sets code as text and not selected value.

thanks in advance.

Here is a C# to VB converter http://converter.telerik.com/

C#


ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "some value" });


VB


ComboBox1.SelectedItems.Add(New Ext.Net.ListItem() With { _
Key .Value = "some value" _
})

hemantpatil
Aug 24, 2013, 12:01 PM
Please find below code for reference.

what i want to do is, set Dept. 2 as selected record on click of button. it's value is 2 and i want to set it with value only not text.

ComboBox.aspx


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ComboBox.aspx.vb" Inherits="ComboBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<div>
<ext:ComboBox ID="ComboBox1"
runat="server"
FieldLabel="Select a single Company"
ValueField="CODE"
DisplayField="NAME"
Width="600"
LabelWidth="130"
QueryMode="Local"
TypeAhead="true">
<Store>
<ext:Store ID="Store1" runat="server">
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="CODE" />
<ext:ModelField Name="NAME" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
</ext:ComboBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>


ComboBox.aspx.vb

Imports System.Web.UI.WebControls
Imports System.Configuration

Imports ext1 = Ext.Net
Imports Ext.Net
Imports Ext.Net.JSON

Partial Public Class ComboBox
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Store1.DataSource = Me.DeptData
Me.Store1.DataBind()
End Sub

Private ReadOnly Property DeptData() As Object()
Get
Return New Object() {New Object() {1, "Dept. 1"}, New Object() {2, "Dept. 2"}, New Object() {3, "Dept. 3"}, New Object() {4, "Dept. 4"}}
End Get
End Property

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.SelectedItems.Add(New Ext.Net.ListItem("2").Mode = ParameterMode.Value)
End Sub
End Class

Thanks in advance.

Daniil
Aug 26, 2013, 8:28 AM
This appears to be working.

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As Ext.Net.ListItem = New Ext.Net.ListItem("2")
s.Mode = ParameterMode.Raw
ComboBox1.SelectedItems.Add(s)
End Sub

hemantpatil
Aug 26, 2013, 11:03 AM
Hi Dannil,

Thanks for you Reply. your solution worked for me.

my finding regarding this method is, if you try to use this code on click or direct click of Ext.Net Button, it will not work. it works on click of Asp Button only. this is FYI only.

you can close this thread.

Daniil
Aug 26, 2013, 11:22 AM
With a DirectEvent it should be:

Dim s As Ext.Net.ListItem = New Ext.Net.ListItem("2")
s.Mode = ParameterMode.Raw
ComboBox1.SelectedItems.Add(s)
ComboBox1.UpdateSelectedItems()

hemantpatil
Aug 26, 2013, 12:00 PM
Hi Dannil,

Thanks for your quick reply.

ebeker
May 16, 2014, 5:39 AM
With a DirectEvent it should be:

Dim s As Ext.Net.ListItem = New Ext.Net.ListItem("2")
s.Mode = ParameterMode.Raw
ComboBox1.SelectedItems.Add(s)
ComboBox1.UpdateSelectedItems()

Do not forget to include
combobox1.selecteditems.clear at the begining of this code piece.