Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting data from user control 1

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
Hi...

I have a user control that is basically a dropdownlist populate by a database, but its used on many pages...

It display on the pages i use it on fine, and it validates correctly, but i can't seem to get the data out of it in code (on the page the control is *used* on).

I've tried this, among other things, with no luck:
CountryList1.Items[CountryList1.SelectedIndex].Value

here is the control in the page i use it on:
<COUNTRIES:CountryList id=&quot;CountryList1&quot; runat=&quot;server&quot;></COUNTRIES:CountryList>
the tag is registered fine.

here is the user control:
<%@ Control Language=&quot;VB&quot; Debug=&quot;true&quot; %>
<%@ import Namespace=&quot;System.Data&quot; %>
<%@ import Namespace=&quot;System.Data.SqlClient&quot; %>
<script runat=&quot;server&quot;>

Sub Page_Load(sender As Object, e As EventArgs)
ShowCountries()
End Sub

Sub ShowCountries()

Dim conn As SqlConnection
Dim DS as DataSet
Dim sql As SqlDataAdapter
Dim TotalRows, r as Integer

Dim strConn As String = &quot;server=(local);database=BusinessMeetings;user id=sa&quot;
conn = New SQLConnection(strConn)

sql = New SqlDataAdapter(&quot;SELECT countryid, countryname FROM countries ORDER BY countryname&quot;, conn)

DS = new DataSet()
sql.Fill(DS, &quot;countries&quot;)

'total rows for loop
Dim GetRows As DataTable = DS.Tables (&quot;countries&quot;)
TotalRows = GetRows.Rows.Count

'create default and set it
Me.ddCountries.Items.Add(New ListItem(&quot;Choose a Country&quot;, &quot;&quot;))
ddCountries.SelectedIndex = 0

'fill 'er up
For r = 0 To TotalRows-1
Me.ddCountries.Items.Add(New ListItem(GetRows.Rows(r)(1).ToString(), GetRows.Rows(r)(0).ToString()))
Next

conn.Close : conn = Nothing
sql = Nothing : sql = Nothing

End Sub

</script>
<asp:DropDownList id=&quot;ddCountries&quot; runat=&quot;server&quot;></asp:DropDownList>
<asp:RequiredFieldValidator id=&quot;CountryValidator&quot; Runat=&quot;server&quot; ErrorMessage=&quot;Country Required&quot; ControlToValidate=&quot;ddCountries&quot; Display=&quot;None&quot;></asp:RequiredFieldValidator>

any ideas???

THANKS.
 
Remember to treat a user control as what it is. Just another class. One that you created using inheritence and such. So more to the point you need to setup a public property to return the data you want out of the control. This may be simply declaring one of the controls in the user control as public or setting up your own properties so you can be more specific. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I guess i'm not sure how to do that... i've tried a couple things, but i still get:

BC30390: 'ASP.countries_ascx.ddCountries' is not accessible in this context because it is 'Protected'.

any ideas based on the code in the original thread?
 
Ana oop ana oop and here we go.

Public readonly Property SelectedValue() as String
Get
SelectedValue= ddCountries.SelectedItem.Value
End Get
End Property


In the page using the control then you would simply access that property like any other class property.

I do have a suggestion for when you are filling up the list. Alot of controls have datasource and datamember properties. Set the datasource to the dataset you are using then simply call the databind method. This will automaticlly fill up your control for you. After these lines insert your title for the dropdown
Me.ddCountries.Items.Insert(0,New ListItem(&quot;Choose a Country&quot;, &quot;&quot;))


Hope this helps. Feel free to ask if I have muxed things up That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
i think that will work great, and thanks for the tip on filling my list...

however as my luck would have it, now my form won't post. I wonder if its because the ascx and aspx files both have a Page_Load subroutine??? if so, how do i avoid the one in the control? somehow i need to call the showcountries sub... ugh.
 
No it's fine that both have the page_load. Without more info I am not sure why your form wouldn't be posting. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
oh nevermind, it had to do with the validation... I have the validate control set to display=none because of the way i show missing fields, so i couldn't see that it was trying to tell me one of my inputs needed some data!

stupid mistake...

Thanks for all the help! I incorporated the dropdownlist's databind method too and its working great!
 
np glad to help That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top