By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

Dynamically Populate Server Control?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.