Dynamically Populate Server Control?

  • Thread starter Thread starter Arpan
  • Start date Start date
A

Arpan

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
 
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,

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
 
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.
 
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
 
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
 
Back
Top