Friday, March 16, 2012

populating controls

I have a user control. In which i have a dropdown list control. I want to populate this control(dropdownlist) when this control loaded in to the page. So i write a control_load event
<script>
public drpAge As DropDownList

Dim hashAge As new Hashtable()

Sub Control_Load(Sender As Object, E As EventArgs)

hashAge(0) = "Select Age "
hashAge(1) = "Less than 18"
hashAge(2) = "Between 18 & 21"
hashAge(3) = "Between 21 & 25"
hashAge(4) = "Greater than 25"

drpAge.DataSource = hashAge

drpAge.DataBind()

End Sub
</script
control is:

<ASP:DropDownList ID="drpAge" runat="server" /
It is showing dropdown list on the page, but not populating the list.

Plz help me.

& Also tell me how to write code behind file for controls.

I tryied it . I write a vb file(profile.vb) in which i write a class(profile_class) which Inherit Control class . When i write
in control (.ascx) file

<%@dotnet.itags.org. Control language="vb" Inherits="profile_class" src="http://pics.10026.com/?src=prifile.vb" %
It creates Application error

" 'profile_class' is not a valid base class to inherit from "

So plz help me

Thanx in Advance

AmitYou have to tell the hashtable what to display, another way would be to use a Collection instead of a hashtable:

In the Page_Load Sub you could add the following code:


Dim hashAge As New Collection()

hashAge.Add("Select Age ")
hashAge.Add("Less than 18")
hashAge.Add("Between 18 & 21")
hashAge.Add("Between 21 & 25")
hashAge.Add("Greater than 25")

drpAge.DataSource = hashAge

drpAge.DataBind()


Hi Ajay

Thanx
But Its not working
I think you're (quite understandably) confusing server controls and user controls.

First, server controls (such as the DropDownList you are using) do not have a Control_Load method which you can use to "initialise" this control.

So, you might change the name of your sub from

 Sub Control_Load(Sender As Object, E As EventArgs)
to
 Sub drpAge_Initialise()
and then, in your Page_Load method, call this sub:
 Sub Page_Load(Sender As Object, E As EventArgs)
drpAge_Initialise()
End Sub

It's in your second question where you've got the two confused. (Again, understandably ... I think the ASP.NET framework should have changed the name of User Controls to something else ... in fact, they were originally called Pagelets.)

A codebehind for aUser control must inheritSystem.Web.UI.Web.UserControl.

Conversely, the code for aserver control generally inherits either fromSystem.Web.UI.Control or fromSystem.Web.UI.WebControl.

In the sample code you've shown, you're trying to create a codebehind for a User Control.

Does that help?
Thanx , Actually I am new to ASP.Net

I got it. Actually to create a user control i inhering Control class instead of UserControl.

Its working now.

Amit :)

0 comments:

Post a Comment