472,103 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,103 software developers and data experts.

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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
End If
lblMessage.DataBind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" SelectionMode="single"
runat="server"/>
<asp:Label id="lblMessage" Text=<%# lbColors.SelectedItem.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.SelectedItem.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.DataBind()

is replaced with

lbColors.DataBind()

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 1772
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******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
End If
lblMessage.DataBind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" SelectionMode="single"
runat="server"/>
<asp:Label id="lblMessage" Text=<%# lbColors.SelectedItem.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.SelectedItem.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.DataBind()

is replaced with

lbColors.DataBind()

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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
lbColors.DataBind()

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

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" 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******@hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
End If
lblMessage.DataBind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" SelectionMode="single"
runat="server"/>
<asp:Label id="lblMessage" Text=<%# lbColors.SelectedItem.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.SelectedItem.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.DataBind()

is replaced with

lbColors.DataBind()

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.DataBind() 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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
End If
lblMessage.DataBind()
End Sub
</script>

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" SelectionMode="single"
runat="server"/>
<asp:Label id="lblMessage" Text=<%# lbColors.SelectedItem.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.SelectedItem.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.DataBind()

is replaced with

lbColors.DataBind()

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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
lbColors.DataBind()

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

<form runat="server">
<asp:ListBox id="lbColors" AutoPostBack="true" 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***@newsgroups.nospamwrote in message
news:%2********************@TK2MSFTNGP03.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.IsPostBack) Then
'create an array of colors
Dim arrColors() As String = {"red", "blue", "green",
"yellow"}

lbColors.DataSource = arrColors
lbColors.SelectedIndex = 0
lbColors.DataBind()

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

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

Aug 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Scott C. | last post: by
2 posts views Thread by angus | last post: by
4 posts views Thread by Neil Stevens | last post: by
2 posts views Thread by Steve Franks | last post: by
6 posts views Thread by Chris Davoli | last post: by
reply views Thread by leo001 | last post: by

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.