473,574 Members | 3,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Populate Server Control?

In order to populate any server control with data dynamically, is it
ALWAYS NECESSARY to either BIND the DataSource to that server control
or call the DataBind method of that server control?

For e.g. consider the following code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
End If
lblMessage.Data Bind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>
</form>

The above code generates the following error:

Object reference not set to an instance of an object.

the offending line being

<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>

Is the error thrown because the ListBox named "lbColors" HASN'T been
BOUND to the DataSource?

On the other hand, if the line

lblMessage.Data Bind()

is replaced with

lbColors.DataBi nd()

then the ListBox would get populated with the 4 colors but the Label
named "lblMessage " won't generate any text when the page loads for the
first time or when an item is selected in the ListBox. Is it because
DataBinding Expressions are evaluated only when the DataBind method of
the control is called & since the DataBind method of the Label control
isn't being called anywhere in the code, the DataBinding Expression of
the Label control doesn't get evaluated & hence the Label doesn't
generate any output?

Please correct me if I am wrong.

Thanks,

Arpan

Aug 1 '06 #1
5 1860
Order of binding is the problem. If you are going to bind in codebehind, do
not turn around and declaratively set other controls based on that value.

Choices:
BEST: Load the label from codebehind, as wel
OK: Move data binding of the dropdown to Init. If another value is chosen,
it will be reset after Init to the ViewState value.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
"Arpan" <ar******@hotma il.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
In order to populate any server control with data dynamically, is it
ALWAYS NECESSARY to either BIND the DataSource to that server control
or call the DataBind method of that server control?

For e.g. consider the following code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
End If
lblMessage.Data Bind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>
</form>

The above code generates the following error:

Object reference not set to an instance of an object.

the offending line being

<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>

Is the error thrown because the ListBox named "lbColors" HASN'T been
BOUND to the DataSource?

On the other hand, if the line

lblMessage.Data Bind()

is replaced with

lbColors.DataBi nd()

then the ListBox would get populated with the 4 colors but the Label
named "lblMessage " won't generate any text when the page loads for the
first time or when an item is selected in the ListBox. Is it because
DataBinding Expressions are evaluated only when the DataBind method of
the control is called & since the DataBind method of the Label control
isn't being called anywhere in the code, the DataBinding Expression of
the Label control doesn't get evaluated & hence the Label doesn't
generate any output?

Please correct me if I am wrong.

Thanks,

Arpan

Aug 1 '06 #2
Arpan,

Yes, you need to call databind on the control.

You can't bind from one object to another like you are trying with your
label.

Instead you need to do something more like this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
lbColors.DataBi nd()

lblMessage.Text = lbColors.Select edItem.Text
End If
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " runat="server"/>
</form>
Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Arpan" <ar******@hotma il.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
In order to populate any server control with data dynamically, is it
ALWAYS NECESSARY to either BIND the DataSource to that server control
or call the DataBind method of that server control?

For e.g. consider the following code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
End If
lblMessage.Data Bind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>
</form>

The above code generates the following error:

Object reference not set to an instance of an object.

the offending line being

<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>

Is the error thrown because the ListBox named "lbColors" HASN'T been
BOUND to the DataSource?

On the other hand, if the line

lblMessage.Data Bind()

is replaced with

lbColors.DataBi nd()

then the ListBox would get populated with the 4 colors but the Label
named "lblMessage " won't generate any text when the page loads for the
first time or when an item is selected in the ListBox. Is it because
DataBinding Expressions are evaluated only when the DataBind method of
the control is called & since the DataBind method of the Label control
isn't being called anywhere in the code, the DataBinding Expression of
the Label control doesn't get evaluated & hence the Label doesn't
generate any output?

Please correct me if I am wrong.

Thanks,

Arpan

Aug 1 '06 #3
You are indeed correct.
You have to call DataBind() on the listbox to bind it to the datasource,
you get the error since the listbox has no items.
If you would do lbColors.DataBi nd() before binding the lblMessage
control it will work.

Arpan wrote:
In order to populate any server control with data dynamically, is it
ALWAYS NECESSARY to either BIND the DataSource to that server control
or call the DataBind method of that server control?

For e.g. consider the following code:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
End If
lblMessage.Data Bind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>
</form>

The above code generates the following error:

Object reference not set to an instance of an object.

the offending line being

<asp:Label id="lblMessage " Text=<%# lbColors.Select edItem.Text %>
runat="server"/>

Is the error thrown because the ListBox named "lbColors" HASN'T been
BOUND to the DataSource?

On the other hand, if the line

lblMessage.Data Bind()

is replaced with

lbColors.DataBi nd()

then the ListBox would get populated with the 4 colors but the Label
named "lblMessage " won't generate any text when the page loads for the
first time or when an item is selected in the ListBox. Is it because
DataBinding Expressions are evaluated only when the DataBind method of
the control is called & since the DataBind method of the Label control
isn't being called anywhere in the code, the DataBinding Expression of
the Label control doesn't get evaluated & hence the Label doesn't
generate any output?

Please correct me if I am wrong.

Thanks,

Arpan
Aug 1 '06 #4
Sure you can, the selecteditem property is bindable.
Btw, if you don't update the label on postback the function is kind of
silly really... :X

S. Justin Gengo wrote:
Arpan,

Yes, you need to call databind on the control.

You can't bind from one object to another like you are trying with your
label.

Instead you need to do something more like this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
lbColors.DataBi nd()

lblMessage.Text = lbColors.Select edItem.Text
End If
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " runat="server"/>
</form>
Regards,
Aug 1 '06 #5
Matsl,

Thanks for pointing out my mistake. I'd never tried it that way. I should
have given it a go first.

:)

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
<Ma***@newsgrou ps.nospamwrote in message
news:%2******** ************@TK 2MSFTNGP03.phx. gbl...
Sure you can, the selecteditem property is bindable.
Btw, if you don't update the label on postback the function is kind of
silly really... :X

S. Justin Gengo wrote:
>Arpan,

Yes, you need to call databind on the control.

You can't bind from one object to another like you are trying with your
label.

Instead you need to do something more like this:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBac k) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSo urce = arrColors
lbColors.Select edIndex = 0
lbColors.DataBi nd()

lblMessage.Text = lbColors.Select edItem.Text
End If
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="t rue" SelectionMode=" single"
runat="server"/>
<asp:Label id="lblMessage " runat="server"/>
</form>
Regards,

Aug 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
986
by: Scott C. | last post by:
We are building a web application that allow's the users to change the defaults of a their pages. Here is a list of what I've done so far 1.) Return the control names, control properties and default values back from a database 2.) I load each record into an objec 3.) I then load all of these objects into a collection and send it back to the...
2
1380
by: angus | last post by:
Hi All, i have an aspx form for the user to input, and those data will be stored into the database. I would like to build up the form dynamically by using the data in the a database table, named question for example, i have a question table and the data in the question table is as follow
4
1704
by: Neil Stevens | last post by:
Hi, Merry Christmas to you all. I have a problem with server controls, i am writing a C# ASP.NET web project and i am creating custom server controls for some of the more common element. One example is a NavigationMenu control or a Login control. The problem is that when the page renders, say, the Login control, the ID of the username...
7
2980
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
2080
by: Steve Franks | last post by:
In ASP.NET 2.0 you can now apparently do this: <asp:label runat="server" text="some browser" IE:text="any IE browser" IE5:text="the IE 5 browser" PIE:text="the Pocket PC browser" /> Now the tokens "IE5" and "PIE" are coming from the updated browser capabilities component. I'd like to do the same thing, but create my own tokens that have...
6
2133
by: Chris Davoli | last post by:
How do I dynamically add web server controls to a web form? Also, how can I position them? Any sample code or links would be appreciated... -- Chris Davoli
2
4909
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is being updated by some other process through remoting. When the page loads, I init the tree, and in my browser I can see the initialized tree. The...
3
6882
by: Yi Chen | last post by:
We have a drop down list on a PHP page, with several product names, and when people click one item, we will refresh the same page with the product name as parameter, and in turn we want to include a HTML file into the content area of the same page. I know it is recommended to put everything into database, but we want the web site to be very...
7
6644
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: ...
0
7842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8110
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6514
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3775
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.