User Control... one strange problem…

Hi,

One strange problem…

I’ve developed one user control for all countries list. Here is the code…

CountryControlBase.vb in App_Code folder
---------------------------------------
Imports Microsoft.VisualBasic
Namespace BlueSky.UserControls
    Public Class CountryControlBase
        Inherits UserControl
        Public Sub New()
            MyBase.New()
        End Sub
        Public Overridable Property Text() As String
            Get
                Return "selected value"
            End Get
            Set(ByVal value As String)
                'child class will override this implementation...
            End Set
        End Property
    End Class
End Namespace
----------------------------------------

--------------------------
CountryControl.ascx
---------------------
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CountryControl.ascx.vb" Inherits="CountryControl" %>

<asp:DropDownList ID=ddlCountries Runat=server
EnableViewState=True></asp:DropDownList>

<asp:RequiredFieldValidator ID="ddlCountriesRequiredFieldValidator" ControlToValidate="ddlCountries" runat="server" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
---------------------------------------------------

---------------------------------------------------
CountryControl.ascx.vb
-----------------------------------------------
Imports System.Xml
Imports System.Data
Partial Class CountryControl
    Inherits BlueSky.UserControls.CountryControlBase

    Public Sub New()
        MyBase.new()
    End Sub

    Protected _validate As Boolean

    Public Property Validate() As Boolean
        Get
            Return _validate
        End Get
        Set(ByVal value As Boolean)
            _validate = value
        End Set
    End Property

    Public Overrides Property Text() As String
        Get
            Return ddlCountries.SelectedValue
        End Get
        Set(ByVal Value As String)
            ddlCountries.SelectedValue = Value
        End Set
    End Property

    Private Function UCFirst(ByVal str As String) As String
        str = str.ToLower
        Dim chrFirst As Char
        chrFirst = CType(str.Substring(0, 1), Char)
        chrFirst = CType(chrFirst.ToString.ToUpper, Char)
        str = chrFirst & str.Substring(1, str.Length - 1)

        Return str

    End Function

    Private Function ProperCaseFormat(ByVal str As String) As String
        Return StrConv(str, VbStrConv.ProperCase)
    End Function

    Private Sub generateCountries()
        Dim ds As New DataSet
        Dim i As Integer
        Try
            ds.ReadXml(MapPath("countries.xml"))
            ddlCountries.Items.Add("")
            For i = 0 To ds.Tables(0).Rows.Count - 1
                ddlCountries.Items.Add(ProperCaseFormat(CType(ds.Tables(0).Rows(i).Item(0), String).ToLower))
            Next

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

        ddlCountries.SelectedValue = Text
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        generateCountries()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        ddlCountriesRequiredFieldValidator.Enabled = _validate
    End Sub

End Class
-----------------------------------------------------------------------------------------------------------

Now I am using this user control in one of my aspx page like this…
The register statement at the top of the page…
-------------------------
Countries.aspx
-------------------------
<%@ Register TagPrefix="uc" TagName="Country" Src="~/UserControls/Country/CountryControl.ascx" %>
----------------------------
and in between like this...
-------------------------
<uc:Country Id="ddlCountries" runat=Server Validate=False></uc:Country>
--------------------------

Now the problem is that, when I am trying to access this control in codefile…
---------------------
Countries.aspx.vb
---------------------
 Dim c As Control
        c = CType(Me.FindControl("ddlCountries"), BlueSky.UserControls.CountryControlBase)
 Dim country as String
 country = c.Text
-------------------------

But mysteriously… the FindControl is not returning a reference to the control added in the web page and I am getting a “Object reference not set to an instance of an object.” Error.

I am using VB.Net as language and .Net Framework 2.0 with Visual Studio 2005 beta 2 releases.


Please help me…

Thanks,
-Lalit

 

 

IS your aspx page inside master page ?if so you need find control inisde master like this.Page.Master.FindControl("ddlCountries");

Also try and find "ddlCountries" in page source for making sure its present somewhere :)

thoughts on "User Control... one strange problem…"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>