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

ASP.NET 2.0: Bind ArrayList to FormView Control

P: n/a
Hello,

I am trying to bind an arraylist to a FormView DropDownList control in the
PreRender state. The error that I get is the following:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in
the context of a databound control. Any help with this would be
appreciated.
--
Thanks in advance,

sck10
ASP.NET file
--------------
<td style="width:70%; text-align:left;">
<asp:DropDownList id="ddlRevenueYearInsert" Runat="Server"
SelectedValue='<%# Bind("RevenueYear") %>'/></td>
</tr>

CodeBehind File
-----------------
Protected Sub fvCustRev_PreRender(ByVal sender As Object, ByVal e As
EventArgs) Handles fvCustRev.PreRender
If Me.fvCustRev.CurrentMode = FormViewMode.Edit Then
Me.hdnPresentMode.Value = "fvCustRev.Edit"
Call Me.YearLoop()
End If
End Sub
Private Sub YearLoop()
'Use Array to build DropDownList with For Loop
ctrFor = 0
Dim arrFor As ArrayList = New ArrayList()
For ctrFor = Year(Now()) - 3 To Year(Now()) + 10
arrFor.Add(ctrFor)
Next
arrFor.TrimToSize()
'Assign list to FormView datacontrol
If Me.hdnPresentMode.Value = "fvCustRev.Edit" Then
CType(Me.fvCustRev.FindControl("ddlRevenueYearEdit "),
DropDownList).DataSource = arrFor
CType(Me.fvCustRev.FindControl("ddlRevenueYearEdit "),
DropDownList).DataBind()
End If

End Sub
Apr 27 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Sck10,

Thank you for posting.

From your description, you're encountering some exception when try to
dynamically bind an ArrayList to the DropDownList control(in a formview?).

As for the FormView and dropdownlist, would you provide the complete page
markup (aspx) so that we can get the complete view of the two controls and
their relation ship. Also, as for intercept the dropdownlist and perform
further databinding, you can use the FormView's "DataBound" event instead
of PreRender, this event fires after the formview has performed databinding
operation. And I think it is better than the PreRender event.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 28 '06 #2

P: n/a
Hi Steven,

Actually, I am creating the ArrayList in the CodeBehind and then trying to
bind the FormView Control to it. The ASPX text for the FormView
(<EditItemTemplate>) is at the bottom.

CodeBehind File
-----------------
Protected Sub fvCustRev_PreRender(ByVal sender As Object, ByVal e As
EventArgs) Handles fvCustRev.PreRender
If Me.fvCustRev.CurrentMode = FormViewMode.Edit Then
Me.hdnPresentMode.Value = "fvCustRev.Edit"
Call Me.YearLoop()
End If
End Sub

Private Sub YearLoop()
'Use Array to build DropDownList with For Loop
ctrFor = 0
Dim arrFor As ArrayList = New ArrayList()
For ctrFor = Year(Now()) - 3 To Year(Now()) + 10
arrFor.Add(ctrFor)
Next
arrFor.TrimToSize()
'Assign list to FormView datacontrol
If Me.hdnPresentMode.Value = "fvCustRev.Edit" Then
CType(Me.fvCustRev.FindControl("ddlRevenueYearEdit "),
DropDownList).DataSource = arrFor
CType(Me.fvCustRev.FindControl("ddlRevenueYearEdit "),
DropDownList).DataBind()
End If

End Sub
Here is the FormView for the <EditTemplate>
-----------------------------------------------
<asp:FormView
ID="fvCustRev"
runat="server"
DataKeyNames="CustomerRevenue_ID"
DataSourceID="dsDetail"
OnItemUpdating="fvCustRev_ItemUpdating_Validate"
OnItemUpdated="fvCustRev_ItemUpdated_Trans"
OnItemInserting="fvCustRev_ItemInserting_Validate"
DefaultMode="Edit"
style="width:70%">

<EditItemTemplate>
<table class="tblOutline" style="width:100%" border="1px">
<tr>
<th colspan="2"><asp:Label ID="lblTitleEdit" Text="Customer Revenue:
Edit" runat="server" /></th>
</tr>

<tr>
<td style="width:30%; text-align:right;" class="BlkB">Revenue
Year:&nbsp;</td>
<td style="width:70%; text-align:left;">
<asp:DropDownList id="ddlRevenueYearEdit" Runat="Server"
DataTextField="CategoryType_ID" DataValueField="CategoryType_ID"
SelectedValue='<%# Bind("RevenueYear") %>'/></td>
</tr>

<tr>
<td style="width:30%; text-align:right;"
class="BlkB">Customer:&nbsp;</td>
<td style="width:70%; text-align:left;">
<asp:TextBox ID="txtCustomerEdit" Text='<%# Bind("Customer") %>'
Width="200px" runat="server" /></td>
</tr>

<tr>
<td style="width:30%; text-align:right;" class="BlkB">Region:&nbsp;</td>
<td style="width:70%; text-align:left;">
<asp:DropDownList id="ddlRegionEdit" DataSourceID="dsRegionDLL"
Runat="Server"
DataTextField="CategoryType_ID" DataValueField="CategoryType_ID"
SelectedValue='<%# Bind("Region") %>'/></td>
</tr>

<tr>
<td style="width:30%; text-align:right;" class="BlkB">Annual
Rev:&nbsp;</td>
<td style="width:70%; text-align:left;">
<asp:TextBox ID="txtAnnualRevenueEdit" Text='<%# Bind("AnnualRevenue",
"{0:c0}") %>' Width="100px" runat="server" /></td>
</tr>

<tr>
<td colspan="2">
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update" Text="Update" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" /></td>
</tr>

</table><br />
</EditItemTemplate>


"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:cx**************@TK2MSFTNGXA01.phx.gbl...
Hi Sck10,

Thank you for posting.

From your description, you're encountering some exception when try to
dynamically bind an ArrayList to the DropDownList control(in a formview?).

As for the FormView and dropdownlist, would you provide the complete page
markup (aspx) so that we can get the complete view of the two controls and
their relation ship. Also, as for intercept the dropdownlist and perform
further databinding, you can use the FormView's "DataBound" event instead
of PreRender, this event fires after the formview has performed databinding operation. And I think it is better than the PreRender event.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 28 '06 #3

P: n/a
Thanks for your response Sck10,

From the further code snippet, I've got the things you want to do. So you
just want to use the ArrayList to populate the DropDownList in the
EditTemplate of the FormView control ,correct? If so, I think you can just
use the event of the DropDownList itself(instead of using FormView's event)
to perform the databinding. For example, you can use the DropDownList's
Init event to populate the list items. e.g:

=========aspx============
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<EditItemTemplate>
.............................
<asp:DropDownList ID="DropDownList1" runat="server"
OnInit="DropDownList1_Init">
</asp:DropDownList><br />
..................................
</EditItemTemplate>

==========code behind===============
protected void DropDownList1_Init(object sender, EventArgs e)
{
DropDownList list = sender as DropDownList;

//replace below with your arrayList code here...

list.DataTextField = "CategoryName";
list.DataValueField = "CategoryID";
list.DataSource =
SqlDataSource2.Select(DataSourceSelectArguments.Em pty);
list.DataBind();
}
========================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
May 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.