G
Graham Barlow
I have created a Web Control Library and I have created a required drop down
list control.
The control is basically a drop down list control and a required validator
control. When I use the control on a page I cannot get the value from the
selected item in the drop down when the user clicks a submit button. I have
included the code for the user control and the test page i have created.
Does anyone have any idea where i'm going wrong???
Thanks
Graham
User Control code
<-- code starts -->
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace myCustomControls
Public Class ReqDropDown
Inherits Control
Implements INamingContainer
Private myLabel As Label
Private myDropDown As DropDownList
Private myRequiredFieldValidator As RequiredFieldValidator
Private _errormessage As String
Private _requiredfieldindicator As String = "*"
Private _passwordfield As Boolean = False
Public Property RequiredFieldIndicator() As String
Get
Return _requiredfieldindicator
End Get
Set(ByVal Value As String)
_requiredfieldindicator = Value
End Set
End Property
Public Property DataValueField() As String
Get
Return myDropDown.DataValueField
End Get
Set(ByVal Value As String)
myDropDown.DataValueField = Value
End Set
End Property
Public Property DataTextField() As String
Get
Return myDropDown.DataTextField
End Get
Set(ByVal Value As String)
myDropDown.DataTextField = Value
End Set
End Property
Public Property SelectedValue() As String
Get
Return myDropDown.SelectedValue
End Get
Set(ByVal Value As String)
myDropDown.SelectedValue = Value
End Set
End Property
Public ReadOnly Property SelectedItem() As ListItem
Get
Return myDropDown.SelectedItem
End Get
End Property
Public Property SelectedIndex() As Integer
Get
Return myDropDown.SelectedIndex
End Get
Set(ByVal Value As Integer)
myDropDown.SelectedIndex = Value
End Set
End Property
Public Property DataMember() As String
Get
Return myDropDown.DataMember
End Get
Set(ByVal Value As String)
myDropDown.DataMember = Value
End Set
End Property
Public Property DataSource() As DataSet
Get
Return myDropDown.DataSource
End Get
Set(ByVal Value As DataSet)
myDropDown.DataSource = Value
With myDropDown
.DataBind()
End With
End Set
End Property
Public Property CssClass() As String
Get
Return myDropDown.CssClass
End Get
Set(ByVal Value As String)
myDropDown.CssClass = Value
End Set
End Property
Public Property RequiredFieldIndicatorCssClass() As String
Get
Return myLabel.CssClass
End Get
Set(ByVal Value As String)
myLabel.CssClass = Value
End Set
End Property
Public Property TabIndex() As Short
Get
Return myDropDown.TabIndex
End Get
Set(ByVal Value As Short)
myDropDown.TabIndex = Value
End Set
End Property
Public Property ErrorMessage() As String
Get
Return _errormessage
End Get
Set(ByVal Value As String)
_errormessage = Value
End Set
End Property
Public Sub SetSelected(ByVal Value As String)
Dim i As Long
EnsureChildControls()
If myDropDown.Items.Count > 0 Then
For i = 0 To myDropDown.Items.Count
If myDropDown.Items(i).Value.ToString = Value Then
myDropDown.SelectedIndex = i
Exit For
End If
Next
End If
End Sub
Protected Overrides Sub CreateChildControls()
With myDropDown
.ID = "ddl"
Dim objItem As New ListItem
objItem.Text = "Please select a value"
objItem.Value = ""
.Items.Insert(0, objItem)
End With
Controls.Add(myDropDown)
With myLabel
.ID = "lbl"
.Text = " " + _requiredfieldindicator
End With
Controls.Add(myLabel)
With myRequiredFieldValidator
.ID = "rfv"
.ControlToValidate = myDropDown.ID
.Text = _requiredfieldindicator
.Display = ValidatorDisplay.None
.ErrorMessage = _errormessage
End With
Controls.Add(myRequiredFieldValidator)
End Sub
Public Sub New()
myDropDown = New DropDownList
With myDropDown
.CssClass = "formdata"
End With
myLabel = New Label
With myLabel
.CssClass = "formerror"
End With
myRequiredFieldValidator = New RequiredFieldValidator
With myRequiredFieldValidator
.CssClass = "formerror"
End With
End Sub
End Class
End Namespace
<-- code ends -->
Test page code
<-- code starts -->
Public Class WebForm1
Inherits System.Web.UI.Page
#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 ReqDropDown1 As
WebControlLibrary1.myCustomControls.ReqDropDown
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents ValidationSummary1 As
System.Web.UI.WebControls.ValidationSummary
Protected WithEvents lblSelectedValue As System.Web.UI.WebControls.Label
'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 IsPostBack Then
Else
Dim table As DataTable = New DataTable("Data")
Dim Col1 As DataColumn = New DataColumn("userid",
Type.GetType("System.Int32"))
Dim Col2 As DataColumn = New DataColumn("user",
Type.GetType("System.String"))
table.Columns.Add(Col1)
table.Columns.Add(Col2)
Dim aRow As DataRow = table.NewRow()
aRow(0) = 1
aRow(1) = "user 1"
table.Rows.Add(aRow)
aRow = table.NewRow()
aRow(0) = 2
aRow(1) = "user 2"
table.Rows.Add(aRow)
aRow = table.NewRow()
aRow(0) = 3
aRow(1) = "user 3"
table.Rows.Add(aRow)
Dim ds As New DataSet
ds.Tables.Add(table)
With ReqDropDown1
.DataMember = "Data"
.DataValueField = "userid"
.DataTextField = "user"
.DataSource = ds
End With
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
lblSelectedValue.Text = ReqDropDown1.SelectedValue
End Sub
End Class
<-- code ends -->
list control.
The control is basically a drop down list control and a required validator
control. When I use the control on a page I cannot get the value from the
selected item in the drop down when the user clicks a submit button. I have
included the code for the user control and the test page i have created.
Does anyone have any idea where i'm going wrong???
Thanks
Graham
User Control code
<-- code starts -->
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace myCustomControls
Public Class ReqDropDown
Inherits Control
Implements INamingContainer
Private myLabel As Label
Private myDropDown As DropDownList
Private myRequiredFieldValidator As RequiredFieldValidator
Private _errormessage As String
Private _requiredfieldindicator As String = "*"
Private _passwordfield As Boolean = False
Public Property RequiredFieldIndicator() As String
Get
Return _requiredfieldindicator
End Get
Set(ByVal Value As String)
_requiredfieldindicator = Value
End Set
End Property
Public Property DataValueField() As String
Get
Return myDropDown.DataValueField
End Get
Set(ByVal Value As String)
myDropDown.DataValueField = Value
End Set
End Property
Public Property DataTextField() As String
Get
Return myDropDown.DataTextField
End Get
Set(ByVal Value As String)
myDropDown.DataTextField = Value
End Set
End Property
Public Property SelectedValue() As String
Get
Return myDropDown.SelectedValue
End Get
Set(ByVal Value As String)
myDropDown.SelectedValue = Value
End Set
End Property
Public ReadOnly Property SelectedItem() As ListItem
Get
Return myDropDown.SelectedItem
End Get
End Property
Public Property SelectedIndex() As Integer
Get
Return myDropDown.SelectedIndex
End Get
Set(ByVal Value As Integer)
myDropDown.SelectedIndex = Value
End Set
End Property
Public Property DataMember() As String
Get
Return myDropDown.DataMember
End Get
Set(ByVal Value As String)
myDropDown.DataMember = Value
End Set
End Property
Public Property DataSource() As DataSet
Get
Return myDropDown.DataSource
End Get
Set(ByVal Value As DataSet)
myDropDown.DataSource = Value
With myDropDown
.DataBind()
End With
End Set
End Property
Public Property CssClass() As String
Get
Return myDropDown.CssClass
End Get
Set(ByVal Value As String)
myDropDown.CssClass = Value
End Set
End Property
Public Property RequiredFieldIndicatorCssClass() As String
Get
Return myLabel.CssClass
End Get
Set(ByVal Value As String)
myLabel.CssClass = Value
End Set
End Property
Public Property TabIndex() As Short
Get
Return myDropDown.TabIndex
End Get
Set(ByVal Value As Short)
myDropDown.TabIndex = Value
End Set
End Property
Public Property ErrorMessage() As String
Get
Return _errormessage
End Get
Set(ByVal Value As String)
_errormessage = Value
End Set
End Property
Public Sub SetSelected(ByVal Value As String)
Dim i As Long
EnsureChildControls()
If myDropDown.Items.Count > 0 Then
For i = 0 To myDropDown.Items.Count
If myDropDown.Items(i).Value.ToString = Value Then
myDropDown.SelectedIndex = i
Exit For
End If
Next
End If
End Sub
Protected Overrides Sub CreateChildControls()
With myDropDown
.ID = "ddl"
Dim objItem As New ListItem
objItem.Text = "Please select a value"
objItem.Value = ""
.Items.Insert(0, objItem)
End With
Controls.Add(myDropDown)
With myLabel
.ID = "lbl"
.Text = " " + _requiredfieldindicator
End With
Controls.Add(myLabel)
With myRequiredFieldValidator
.ID = "rfv"
.ControlToValidate = myDropDown.ID
.Text = _requiredfieldindicator
.Display = ValidatorDisplay.None
.ErrorMessage = _errormessage
End With
Controls.Add(myRequiredFieldValidator)
End Sub
Public Sub New()
myDropDown = New DropDownList
With myDropDown
.CssClass = "formdata"
End With
myLabel = New Label
With myLabel
.CssClass = "formerror"
End With
myRequiredFieldValidator = New RequiredFieldValidator
With myRequiredFieldValidator
.CssClass = "formerror"
End With
End Sub
End Class
End Namespace
<-- code ends -->
Test page code
<-- code starts -->
Public Class WebForm1
Inherits System.Web.UI.Page
#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 ReqDropDown1 As
WebControlLibrary1.myCustomControls.ReqDropDown
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents ValidationSummary1 As
System.Web.UI.WebControls.ValidationSummary
Protected WithEvents lblSelectedValue As System.Web.UI.WebControls.Label
'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 IsPostBack Then
Else
Dim table As DataTable = New DataTable("Data")
Dim Col1 As DataColumn = New DataColumn("userid",
Type.GetType("System.Int32"))
Dim Col2 As DataColumn = New DataColumn("user",
Type.GetType("System.String"))
table.Columns.Add(Col1)
table.Columns.Add(Col2)
Dim aRow As DataRow = table.NewRow()
aRow(0) = 1
aRow(1) = "user 1"
table.Rows.Add(aRow)
aRow = table.NewRow()
aRow(0) = 2
aRow(1) = "user 2"
table.Rows.Add(aRow)
aRow = table.NewRow()
aRow(0) = 3
aRow(1) = "user 3"
table.Rows.Add(aRow)
Dim ds As New DataSet
ds.Tables.Add(table)
With ReqDropDown1
.DataMember = "Data"
.DataValueField = "userid"
.DataTextField = "user"
.DataSource = ds
End With
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
lblSelectedValue.Text = ReqDropDown1.SelectedValue
End Sub
End Class
<-- code ends -->