ASP.NET 2.0: Bind ArrayList to FormView Control

S

sck10

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
 
S

Steven Cheng[MSFT]

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.)
 
S

sck10

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

Steven Cheng[MSFT]

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.Empty);
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top