locked
How to access value of dropdownlist in detailsview (template RRS feed

  • Question

  • User317370985 posted

    Hi

    I've created a detailsview with regular textfield inputs. Below those regular inputs I have 1 dropdownlist, which I've added using templates.

    I can access the regular fields using the "edit field" window (via smarttag) and then give a Datafieldname to that textfield. But there is no "Datafield' option for the dropdownlist. 

    How do I get the value of my dropdownlist to the function of my app-code class?

    For pictures and the same question:  http://stackoverflow.com/questions/27825739/how-to-access-value-of-dropdownlist-in-detailsview-template

    Wednesday, January 7, 2015 2:15 PM

Answers

  • User61956409 posted

    Hi Ravar,

    Thanks for your post.

    You could refer to the following sample.

    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="false" OnDataBound="DetailsView1_DataBound">
        <Fields>
            <asp:TemplateField HeaderText="Restaurantnaam">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" DataTextField="Restaurantnaam" DataValueField="ID"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Naam recensent">
                <ItemTemplate>
                    <asp:TextBox ID="txtnaam" runat="server" Text='<%#Eval("Naamrecensent") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Datum recensie">
                <ItemTemplate>
                    <asp:TextBox ID="txtdatum" runat="server" Text='<%#Eval("Datumrecensie") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Beoordeling">
                <ItemTemplate>
                    <asp:TextBox ID="txtbeoordeling" runat="server" Text='<%#Eval("Beoordeling") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Score">
                <ItemTemplate>
                    <asp:TextBox ID="txtscore" runat="server" Text='<%#Eval("Score") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView> 
    

    If you have any question about my reply, please let me know.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 1:55 AM

All replies

  • User-1716253493 posted

    To bind ddl to detailsview datasource you can bind ddl selectedvalue property

    SelectedValue = '<%# Bind("FK_RestaurantNr") %>'

    To populate dropdown items, you can add ddldatasource then set datatextfield and datavaluefield

    Wednesday, January 7, 2015 8:48 PM
  • User61956409 posted

    Hi Ravar,

    Thanks for your post.

    You could refer to the following sample.

    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="false" OnDataBound="DetailsView1_DataBound">
        <Fields>
            <asp:TemplateField HeaderText="Restaurantnaam">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" DataTextField="Restaurantnaam" DataValueField="ID"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Naam recensent">
                <ItemTemplate>
                    <asp:TextBox ID="txtnaam" runat="server" Text='<%#Eval("Naamrecensent") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Datum recensie">
                <ItemTemplate>
                    <asp:TextBox ID="txtdatum" runat="server" Text='<%#Eval("Datumrecensie") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Beoordeling">
                <ItemTemplate>
                    <asp:TextBox ID="txtbeoordeling" runat="server" Text='<%#Eval("Beoordeling") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Score">
                <ItemTemplate>
                    <asp:TextBox ID="txtscore" runat="server" Text='<%#Eval("Score") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView> 
    

    If you have any question about my reply, please let me know.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 8, 2015 1:55 AM
  • User317370985 posted

    @oned_gk

    Where should I put this? Can you give me a little bit more information please? :)

    Thursday, January 8, 2015 2:06 PM
  • User317370985 posted

    This is my code at the moment:

    <asp:DetailsView ID="DetailsView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="odsRecensies" DefaultMode="Insert" ForeColor="Black" GridLines="Horizontal" Height="50px" Width="125px" AutoGenerateRows="False" OnItemInserted="DetailsView1_ItemInserted" OnPageIndexChanging="DetailsView1_PageIndexChanging" OnItemUpdating="DetailsView1_ItemUpdating">
                <EditRowStyle BackColor="#3286DC" Font-Bold="True" ForeColor="White" />
                <Fields>
                    <asp:TemplateField HeaderText="FK Restaurant">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FK_RestaurantNr") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="odsRestaurants" DataTextField="RestaurantNaam" DataValueField="PK_RestaurantNr">
                            </asp:DropDownList>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("FK_RestaurantNr") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="RecensentNaam" HeaderText="Naam recensent" />
                    <asp:BoundField DataField="RecensieDatum" HeaderText="Datum recensie" />
                    <asp:BoundField DataField="Beoordeling" HeaderText="Beoordeling" />
                    <asp:BoundField DataField="Score" HeaderText="Score" />
                    <asp:CommandField ShowInsertButton="True" />
                </Fields>
                <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            </asp:DetailsView>



    Thursday, January 8, 2015 2:08 PM
  • User317370985 posted

    Hi Ravar,

    Thanks for your post.

    You could refer to the following sample.

    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="false" OnDataBound="DetailsView1_DataBound">
        <Fields>
            <asp:TemplateField HeaderText="Restaurantnaam">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl" runat="server" DataTextField="Restaurantnaam" DataValueField="ID"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Naam recensent">
                <ItemTemplate>
                    <asp:TextBox ID="txtnaam" runat="server" Text='<%#Eval("Naamrecensent") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Datum recensie">
                <ItemTemplate>
                    <asp:TextBox ID="txtdatum" runat="server" Text='<%#Eval("Datumrecensie") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Beoordeling">
                <ItemTemplate>
                    <asp:TextBox ID="txtbeoordeling" runat="server" Text='<%#Eval("Beoordeling") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Score">
                <ItemTemplate>
                    <asp:TextBox ID="txtscore" runat="server" Text='<%#Eval("Score") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView> 
    

    If you have any question about my reply, please let me know.

    Best Regards,

    Fei Han

    What does OnDataBound do exactly? I'm currently trying this, but an error occurs when I run this. 

    The error:

    "ASP.AllRecensies_aspx doesn't contain a definition for DetailsView_Databound"

    So what code should I put in "DetailsView_Databound"?

    Thursday, January 8, 2015 2:09 PM
  • User61956409 posted

    Hi Ravar,

    I bind a data source to DropDownList in DetailsView DataBound event.

    Best Regards,

    Fei Han

    Thursday, January 8, 2015 8:41 PM