|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Accessing FormView template controlsI'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. This demo would explain to you the strategies on both the FormView and the
GridView: http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx Show quoteHide quote "Arturo Buonanni" wrote: > 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. > 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. On Tue, 4 Apr 2006 15:55:01 -0700, Phillip Williams <WEBSWAPP@newsgroups.nospam> wrote: Show quoteHide quote >This demo would explain to you the strategies on both the FormView and the >GridView: >http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx 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. Show quoteHide quote "Arturo Buonanni" wrote: > 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. > > On Tue, 4 Apr 2006 15:55:01 -0700, Phillip Williams > <WEBSWAPP@newsgroups.nospam> wrote: > > >This demo would explain to you the strategies on both the FormView and the > >GridView: > >http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx > 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.
--- Posted via www.DotNetSlackers.com
"Cannot have multiple items selected in a DropDownList" Error
How to get ListItemValue and ListItemText from a Combo using SendMessage edit attribute values of a web control just before control rendered to the page (control's Load even clicked event never fires Atlas UpdatePanel with Webparts events inheritance checkbox bind with 0 = checked? Removing columns from Gridview(2.0) databinding on templatefileds dont work? Problems with dynamically created GridView/DetailsView Rendering a Treeview Control |
|||||||||||||||||||||||