Ask a Question related to ASP.NET Building Controls, Design and Development.
-
stephg #1
Web User Control Drop Down List does not show selected item
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
......
======
stephg Guest
-
How To Dynamically Set Selected Item in SELECT List inDW Object
<-- Cross=posted on DW Application Development forum-- > Hi! I hope I can explain this situation halfway decently... I am creating a DW... -
Get value of drop down list from one user control to another -- raise event?
I have tried just about everything that I can understand. I have 2 user controls on a form. One user control has a drop down list, and the 2nd... -
Submitting selected List/Menu Item??
I got Aalphabets A ? Z as links, a Menu with a pre-selected item and a search button The problem is I just cant get to submit the value of the... -
How to set selected item in drop-down list in datagrid
Donald Welker wrote: SelectedValue='<%# DataBinder.Eval(Container.DataItem,"ABCField") %>' -- Jos -
Setting 'selected' item in bound drop down list
You must set the selected index - meaning, you must know the location of the item that needs to be selected in the list. "ctb"... -
Paul Way #2
RE: Web User Control Drop Down List does not show selected item
You'll have to excuse my lack of having an IDE on my computer so you won't be
able to copy and paste this code...
1)
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 'I do not think you need
this line
ddlCountry.SelectedIndex = i
exit for 'go ahead and exit the for loop for optimization
End If
Next
End Set
End Property
Also, if that doesn't work then try the EnsureVisible property (I know that
is on listviews)
Paul Way Guest
-
stephg #3
RE: Web User Control Drop Down List does not show selected item
Thanks for your answer. But unfortunately this change doesn't do the job
either.
Still the item set as selected is not shown selected in the drop down list
on the final page. I have enhance my control by
===
Public ReadOnly Property GetSelectedIndex() As Integer
Get
Return ddlCountry.SelectedIndex
End Get
End Property
===
And if I debug the calling page ddC1.GetSelectedIndex() returns the correct
value. But still the item is not shown selected in the page.
What am I missing????
"Paul Way" wrote:
> You'll have to excuse my lack of having an IDE on my computer so you won't be
> able to copy and paste this code...
>
>
> 1)
>
> 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 'I do not think you need
> this line
> ddlCountry.SelectedIndex = i
> exit for 'go ahead and exit the for loop for optimization
> End If
> Next
> End Set
> End Property
>
>
> Also, if that doesn't work then try the EnsureVisible property (I know that
> is on listviews)stephg Guest
-
Ken Dopierala Jr. #4
Re: Web User Control Drop Down List does not show selected item
Hi,
I'm pretty sure you can't set an item to Selected if another item in the
list is already selected. Try replacing this line:
ddlCountry.SelectedIndex = 0
With this:
ddlCountry.SelectedItem.Selected = False
Also you rebind your DDL everytime you set it which you shouldn't need to
do. But if you do need to for some reason then make sure you call
ddlCountry.Items.Clear() as the first line of your bindDDL function. Good
luck! Ken.
--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
[url]http://www.webhost4life.com/default.asp?refid=Spinlight[/url]
If you sign up under me and need help, email me.
"stephg" <stephg@discussions.microsoft.com> wrote in message
news:ECD5ED5E-041C-495A-8266-E44FEA830B41@microsoft.com...correct> Thanks for your answer. But unfortunately this change doesn't do the job
> either.
> Still the item set as selected is not shown selected in the drop down list
> on the final page. I have enhance my control by
> ===
> Public ReadOnly Property GetSelectedIndex() As Integer
> Get
> Return ddlCountry.SelectedIndex
> End Get
> End Property
> ===
> And if I debug the calling page ddC1.GetSelectedIndex() returns thewon't be> value. But still the item is not shown selected in the page.
> What am I missing????
>
> "Paul Way" wrote:
>> > You'll have to excuse my lack of having an IDE on my computer so youthat> > able to copy and paste this code...
> >
> >
> > 1)
> >
> > 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 'I do not think you need
> > this line
> > ddlCountry.SelectedIndex = i
> > exit for 'go ahead and exit the for loop for optimization
> > End If
> > Next
> > End Set
> > End Property
> >
> >
> > Also, if that doesn't work then try the EnsureVisible property (I know> > is on listviews)
Ken Dopierala Jr. Guest



Reply With Quote

