Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 29th, 2003, 05:16 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default User Controls

I have a user control (ascx) called _CountryList.
This control binds to a dataSet read from XML that came from a webService (with caching) that provides a list of countries.

This user control is then used on every web form that needs a listbox of countries (eg: Customer Registration, etc.)

So far this is all working great....

QUESTION: How do I access the selected country from within the code behind a web form that has the user control embedded?

CODE SNIPPET: (why does this not work?)
================================================== ===
Public Class Register
  Protected WithEvents Countries As System.Web.UI.UserControl

  Private Sub RegisterBtn_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles RegisterBtn.Click
    If Page.IsValid Then
      'Get the Country
      Dim lbCountry As ListBox = DirectCast(Countries.FindControl("lbCountry"), ListBox)
      Dim Country As String = lbCountry.SelectedItem.Value
      lbCountry = Nothing
  End Sub
End Class
================================================== ===
By my reckoning Country should contain the selected value.
Instead I get: 'Object reference not set to an instance of an object'

Thoughts, anyone ???

Reply With Quote
  #2 (permalink)  
Old July 29th, 2003, 05:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Wien, Wien, Austria.
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

In your ascx code behind file you have to declare all form fields you plan to use in the container Web d\forms as public properties:
Public Property MyListBoxvalue
Get
return ListBox1.SelectedItem.Value
End Get
...
and than u can use MyListBoxvalue variable in .aspx page containing user control.
HTH.

...but the Soon is eclipsed by the Moon
Reply With Quote
  #3 (permalink)  
Old July 29th, 2003, 05:49 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, so I have a public property in the ascx.

What would the syntax be in the code behind the form for accessing it?

Reply With Quote
  #4 (permalink)  
Old July 30th, 2003, 07:36 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Bakerdaw
Default

I'm not sure why you are creating a new listbox when you have the user control?
Does the dataset fill a web control dropdownlist in the ascx user control?
If you have a public property that will display the country that is selected in the user control listbox, you should then be able to access it as follows:

Private Sub RegisterBtn_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles RegisterBtn.Click
Dim Country As String

If Page.IsValid Then
      'Get the Country
      Country = Countries.Country
End Sub

In the ascx you should have an event, lbCountries_SelectedIndexChanged that fires when the user selects a country, which then assigns that selecteditem to the property.




Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls dcct84 C# 2 October 28th, 2007 07:20 PM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
User Controls..... Help please cowboy2066 General .NET 2 July 28th, 2004 09:22 AM
User Controls Duncan Pro VB.NET 2002/2003 2 December 1st, 2003 05:41 AM
User Controls Duncan Pro VB.NET 2002/2003 1 October 27th, 2003 12:51 PM



All times are GMT -4. The time now is 11:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.