Formview and linqdatasource

Discussion in 'ASP .Net Web Controls' started by C.R., Mar 5, 2008.

  1. C.R.

    C.R. Guest

    I have a formview using a datasource and it works just fine in its basic
    format. However, I have some foreign keys I want to dispay the description
    for instead of the key value - just like I do in my gridview. In the
    gridview I use the standard template field as follows:
    <asp:TemplateField HeaderText="District" SortExpression="District.Name">
    <ItemTemplate>
    <%#Eval("District.Name")%>
    </ItemTemplate>
    </asp:TemplateField>

    However when I try something similar in the formview I get no value
    displayed for the name:
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("District.Name") %>' />
    I tried the dropdown list approach using a linqdatasource with Districts to
    handle it (I will want the dropdown in the edit and insert templates - but I
    don't need it in the itemtemplate). When I do this I get the error that it
    can't use a declaritive datasource. I am able to get the dropdown approach
    shown here http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1423 by
    using a sqldatasource, but I was hoping to use the linq approach and not have
    a dropdown on the itemtemplate.
    What is the recommended approach for what I am trying to do?

    Thank you in advance.
    C.R. Cook
     
    C.R., Mar 5, 2008
    #1
    1. Advertising

  2. C.R.

    Phil H Guest

    On 5 Mar, 04:26, C.R. <> wrote:
    > I have a formview using a datasource and it works just fine in its basic
    > format.   However, I have some foreign keys I want to dispay the description
    > for instead of the key value - just like I do in my gridview.  In the
    > gridview I use the standard template field as follows:
    > <asp:TemplateField HeaderText="District" SortExpression="District.Name">
    >    <ItemTemplate>
    >       <%#Eval("District.Name")%>
    >    </ItemTemplate>
    > </asp:TemplateField>
    >
    > However when I try something similar in the formview I get no value
    > displayed for the name:
    > <asp:Label ID="Label1" runat="server" Text='<%# Eval("District.Name") %>' />
    > I tried the dropdown list approach using a linqdatasource with Districts to
    > handle it (I will want the dropdown in the edit and insert templates - butI
    > don't need it in the itemtemplate).  When I do this I get the error thatit
    > can't use a declaritive datasource.  I am able to get the dropdown approach
    > shown herehttp://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1423by
    > using a sqldatasource, but I was hoping to use the linq approach and not have
    > a dropdown on the itemtemplate.
    > What is the recommended approach for what I am trying to do?  
    >
    > Thank you in advance.
    > C.R. Cook


    I am still learning framework v 3 so I don't know much about the
    linqdatasource.

    However I would like to make one observation for your benefit and
    hopefully others about this business of using dropdownlists to select
    foreign key values.

    It's important to realise that even if the main datasource select
    statement includes a descriptive field from a joined table you cannot
    use that same datasource to populate the items in a dropdownlist.

    You need a unique list (as in select distinct) of all possible values
    from the joined table but you wont get that from the main table that
    underlies the formview datasource. Hence the datasource that is linked
    to the dropdownlist items must be separate with a select statement
    involving the joined table on its own (returning the foreign key
    values and descriptive text). However the *SelectedValue* of the
    dropdownlist *can* and *should* be databound to the main datasource so
    that the chosen value is used for updating and inserting the foreign
    key value into the main table.

    Sorry if I'm telling you what you already know but I'm certain many
    people get mixed up over this.
     
    Phil H, Mar 8, 2008
    #2
    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. shapper
    Replies:
    1
    Views:
    485
    shapper
    Feb 19, 2008
  2. Jay Pondy
    Replies:
    9
    Views:
    849
    Jay Pondy
    Mar 28, 2008
  3. Jay Pondy
    Replies:
    0
    Views:
    729
    Jay Pondy
    Apr 4, 2008
  4. Marius Manolea
    Replies:
    0
    Views:
    1,029
    Marius Manolea
    Jun 4, 2008
  5. david
    Replies:
    3
    Views:
    853
    Lloyd Sheen
    Jul 26, 2008
Loading...

Share This Page