S
stephg
Hi,
I have a user control that only contains a drop down list. This ddl is
filled with countries. In my ASPX page the control displays properly and I
can also get the selected item. However, when I want to select an item from
the list by default that is not the first entry in the list, it does not show
properly. It always shows the first item in the list selected (which is the
default for ddls).
When I get the selected value from the list, it shows the proper value. But
it doesn t select it in the UI. What am I missing.
Thanks a lot
steph
Below the code.
======
Imports System.Data.OleDb
Imports System.Configuration
Public Class ddlCountries
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
bindDDL()
End If
End Sub
Private Sub bindDDL()
ddlCountry.DataSource = getCountries()
ddlCountry.DataTextField = "CountryName"
ddlCountry.DataValueField = "CountryCode"
ddlCountry.DataBind()
End Sub
Private Function getCountries() As DataSet
' Set up parameter for stored procedure
Dim connection1 As New
OleDbConnection(ConfigurationSettings.AppSettings.Item("OleDbConnection1.ConnectionString"))
Dim command1 As New OleDbCommand
command1.CommandType = CommandType.StoredProcedure
command1.Connection = connection1
Dim OleDbDataAdapter1 As New
System.Data.OleDb.OleDbDataAdapter(command1)
Dim ds As New DataSet
' Set up stored procedure
command1.CommandText = "GetCountries"
OleDbDataAdapter1.Fill(ds, "Countries")
If ds.Tables(0).Rows.Count > 0 Then
Return ds
Else
Return Nothing
End If
End Function
Public ReadOnly Property GetSelectedValue() As Integer
Get
Return ddlCountry.SelectedValue
End Get
End Property
Public WriteOnly Property SetSelectedValue()
Set(ByVal Value)
bindDDL()
Dim i As Integer
ddlCountry.SelectedIndex = 0
For i = 0 To ddlCountry.Items.Count - 1
If ddlCountry.Items(i).Value = Value Then
ddlCountry.Items(i).Selected = True
Else
ddlCountry.Items(i).Selected = False
End If
Next
End Set
End Property
End Class
======
And the snippet from the ASPX page
======
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="CreateCompany.aspx.vb" Inherits="Manage2Job.CreateCompany"%>
<%@Register TagPrefix="countries" TagName="DDLCountry"
Src="ddlCountries.ascx" %>
.......
<countries:ddlcountry id="ddlC1" runat="server"></countries:ddlcountry>
======
And the code behind
======
.....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
showData()
end if
end sub
.....
Sub showData()
....
' Country
ddlC1.SetSelectedValue = dr("Country")
....
end sub
......
======
I have a user control that only contains a drop down list. This ddl is
filled with countries. In my ASPX page the control displays properly and I
can also get the selected item. However, when I want to select an item from
the list by default that is not the first entry in the list, it does not show
properly. It always shows the first item in the list selected (which is the
default for ddls).
When I get the selected value from the list, it shows the proper value. But
it doesn t select it in the UI. What am I missing.
Thanks a lot
steph
Below the code.
======
Imports System.Data.OleDb
Imports System.Configuration
Public Class ddlCountries
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
bindDDL()
End If
End Sub
Private Sub bindDDL()
ddlCountry.DataSource = getCountries()
ddlCountry.DataTextField = "CountryName"
ddlCountry.DataValueField = "CountryCode"
ddlCountry.DataBind()
End Sub
Private Function getCountries() As DataSet
' Set up parameter for stored procedure
Dim connection1 As New
OleDbConnection(ConfigurationSettings.AppSettings.Item("OleDbConnection1.ConnectionString"))
Dim command1 As New OleDbCommand
command1.CommandType = CommandType.StoredProcedure
command1.Connection = connection1
Dim OleDbDataAdapter1 As New
System.Data.OleDb.OleDbDataAdapter(command1)
Dim ds As New DataSet
' Set up stored procedure
command1.CommandText = "GetCountries"
OleDbDataAdapter1.Fill(ds, "Countries")
If ds.Tables(0).Rows.Count > 0 Then
Return ds
Else
Return Nothing
End If
End Function
Public ReadOnly Property GetSelectedValue() As Integer
Get
Return ddlCountry.SelectedValue
End Get
End Property
Public WriteOnly Property SetSelectedValue()
Set(ByVal Value)
bindDDL()
Dim i As Integer
ddlCountry.SelectedIndex = 0
For i = 0 To ddlCountry.Items.Count - 1
If ddlCountry.Items(i).Value = Value Then
ddlCountry.Items(i).Selected = True
Else
ddlCountry.Items(i).Selected = False
End If
Next
End Set
End Property
End Class
======
And the snippet from the ASPX page
======
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="CreateCompany.aspx.vb" Inherits="Manage2Job.CreateCompany"%>
<%@Register TagPrefix="countries" TagName="DDLCountry"
Src="ddlCountries.ascx" %>
.......
<countries:ddlcountry id="ddlC1" runat="server"></countries:ddlcountry>
======
And the code behind
======
.....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
showData()
end if
end sub
.....
Sub showData()
....
' Country
ddlC1.SetSelectedValue = dr("Country")
....
end sub
......
======