Accessing FormView template controls

  • Thread starter Arturo Buonanni
  • Start date
A

Arturo Buonanni

Hello,

I've a question about how to access FormView template controls.

I've some dropdownlist controls in the EditItem template of a FormView
control. These dropdownlist controls are populated by database table
and the dropdownlist's SelectedValue property is bound to fields in
another table. These fields may be null.

Now, when the EditItem template is used with a record that has null
values in the field bound to the dropdownlist's SelectedValue property
an error is raised cause the dropdownlist doesn't have a value for the
"null" field to be selected.

As for what I know there are two way to address this issue: add a
record with the null value on the tables that populates the
dropdownlists or programmatically add an empty item to the
dropdownlists.

I would prefer the second option (I don't like null values in table
key fields) but I can't find the event that gives me access to the
FormView's EditItem template controls so to add the empty items before
the dropdownlist controls are bound to their fields.

Is there a way to do that?

Thanks for your help.
 
A

Arturo Buonanni

Thanks for the demo. It's apreciated but it doesn't address my issues.
In the demo there's only one dropdownlist control bound to a data
field:

<asp:DropDownList ID="ddlCountry" runat="server"
DataSourceID="odsCountries" AutoPostBack="True"
SelectedValue='<%# Bind("Country") %>'>
</asp:DropDownList>

and the sample doesn't carry any code related to this control.

All my dropdownlist controls are bound to data fields and when the
bound data field is null I get this error:
'ddlCountry' has a SelectedValue which is invalid because it does not
exist in the list of items. Parameter name: value

Anyway I solved the issue by adding a row with the null value to the
datatable returned by the code objects I use as data source.

Thanks for the help.
 
P

Phillip Williams

Actually there are 3 databound dropdownlists control in the sample. The
source code is available from the tab at the bottom of the page (if you have
scrolled to the end of the page). I examined 4 different solutions on the
tab titled "GridView" at the bottom.
 
D

Dabbler

Phillip I'm having the same problem with selected value no t in list. I looked over your code and implemented the DataBound method but I get the exception before the ddl's DataBound method is called. I've tried every event of the ddl to see when its happening but the binding to the FormView's objectdatasource must be happening first

Any suggestions would be appreciated
protected void ddlOfficePark_DataBound( Object sender, EventArgs e )
DropDownList ddl = sender as DropDownList
ddl.Items.Insert( 0, new ListItem( "Choose an Office Complex", "0" ) );
FormViewRow fvRow = (FormViewRow)ddl.NamingContainer
DataRowView drv = (DataRowView)fvRegDetail.DataItem
if ( drv != null )
string strOfficeParkId = drv["OfficeParkId"].ToString()
ddl.ClearSelection()
ListItem li = ddl.Items.FindByValue(strOfficeParkId )
if ( li != null ) li.Selected = true




Any suggestions would be appreciated
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top