Accessing FormView template controls

Discussion in 'ASP .Net Web Controls' started by Arturo Buonanni, Apr 4, 2006.

  1. 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.
    Arturo Buonanni, Apr 4, 2006
    #1
    1. Advertising

  2. This demo would explain to you the strategies on both the FormView and the
    GridView:
    http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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.
    >
    Phillip Williams, Apr 4, 2006
    #2
    1. Advertising

  3. 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
    <> 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
    Arturo Buonanni, Apr 5, 2006
    #3
  4. 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.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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
    > <> 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 Williams, Apr 5, 2006
    #4
  5. Arturo Buonanni

    Dabbler Guest

    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
    Dabbler, Apr 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Horace Nunley

    accessing formview controls

    Horace Nunley, Aug 28, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,108
  2. Nathan Sokalski
    Replies:
    0
    Views:
    400
    Nathan Sokalski
    Jan 11, 2007
  3. justin

    accessing controls in FormView

    justin, Jul 31, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    468
    justin
    Jul 31, 2007
  4. Nathan Sokalski

    Accessing the controls in the PagerTemplate when using FormView

    Nathan Sokalski, Jan 11, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    727
    Nathan Sokalski
    Jan 11, 2007
  5. Nathan Sokalski

    Accessing the controls in the PagerTemplate when using FormView

    Nathan Sokalski, Jan 11, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    266
    Nathan Sokalski
    Jan 11, 2007
Loading...

Share This Page