locked
Display multiple records with FormView RRS feed

  • Question

  • User-2080207941 posted

    Hi,

    I have a  a website that I am currently building and would like to know how to display multiple records with FormView. Below is the code that I have so far. It compiles with no errors and if I change the url (http://localhost:64252/ModelBoundWebForm.aspx?id=EX-BBSI01) to match the ProductCodeID  then it only displays that product, but I would like to display them all. how can I achieve this? Also the product ID is completely different for each product.

    ModelBoundWebForm.aspx File

    <div class="container-fluid my-5 product border-top">
            <div class="row">
                `<fieldset>
                    <asp:Formview ID="FVProduct" runat="server" DataKeyNames="ProductCodeId"
                        RenderOuterTable="false" ItemType="ExtremeIron.Models.Product" SelectMethod="FVProduct_GetItem" >
                        <ItemTemplate>
                            <div class="container-fluid my-5 product border-top">
                                <div class="row">
                                    <div class="col-md-6">
                                        <h4 class="text-left mx-3 my-3"><%# Item.ProductName %></h4>
                                        <p class="text-left mx-3"><%# Item.ProductCodeId %></p>
                                        <p class="text-left mx-3">Price: <%# Item.Price %></p>
                                        <p class="text-left mx-3"><%# Item.Description %></p>
                                        <p class="text-left mx-3">Items in Stock: <%# Item.StockQuantity %></p>
                                        <p class="text-left mx-3">Weight: <%# Item.Weight %></p>
                                        <p class="text-left mx-3">In-use Dimensions: <%# Item.InUseDimensions %></p>
                                        <p class="text-left mx-3">Packed Dimensions: <%# Item.PackedDimensions %></p>
                                        <p class="text-left mx-3">Power: <%# Item.Power %></p>
                                        <p class="text-left mx-3">Steam Rate: <%# Item.SteamRate %></p>
                                        <p class="text-left mx-3">Warranty: <%# Item.Warranty %></p>
                                    </div> 
                                    <div class="col-md-6 my-5 text-center">
                                    <a href='<%# "ModelBoundWebForm.aspx?item=" + Item.ProductCodeId %>' target="_blank">
                                    <img alt='<%# Item.ProductName %>' src='<%# Item.Thumbnail %>'  class="img-thumbnail border-0" />
                                    </a>
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:FormView>
                </fieldset>
            </div>
        </div>

    ModelBoundWebForm.aspx.cs Code behind C# file

    public Models.Product FVProduct_GetItem()
            {
    
                string id = Request.QueryString["id"];
                Models.Product product = null;
                if(!string.IsNullOrWhiteSpace(id))
                {
                    var db = new Models.ExtremeIronDBEntities();
                    product = db.Products.Find(id);
                }
    
                return product;
            }

    Sunday, May 12, 2019 2:45 AM

All replies

  • User-943250815 posted

    Switch to Repeater or Datalist control, Formview is made to show one record at a time.
    For Reapeater and DataList check this https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/displaying-data-with-the-datalist-and-repeater/displaying-data-with-the-datalist-and-repeater-controls-cs

    Sunday, May 12, 2019 12:56 PM
  • User-1038772411 posted

    Hi, Karlossimo

    You can use following ItemTemplate to make next row in new line.

    <ItemTemplate> 
       <div> 
         rollno: <asp:Label ID="itemLabel" runat="server" Text='<%# Bind("rollno") %>'></asp:Label>
         name: <asp:Label ID="imageLabel" runat="server" Text='<%# Bind("name") %>'> </asp:Label>
       </div>
    </ItemTemplate>

    But ,You Can Switch Datalist instead of formviewm, below is complete example code.

    Please Refer Below Link 

    https://forums.asp.net/t/1618927.aspx?Display+Multiple+Records+with+Formview

    Monday, May 13, 2019 8:26 AM
  • User665608656 posted

    Hi Karlossimo,

    FormView is to display the values of a single record from a data source using user-defined templates. You can refer to this link:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.formview?view=netframework-4.8

    As the previous two respondents said,I also recommend using the DataList control to show the detailed data by clicking something. 

    I made a demo with Datalist control to show detailed data, you can refer to it. 

    <form id="form1" runat="server">
            <asp:DataList ID="DataList1" runat="server" BackColor="LightGoldenrodYellow"
                BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" OnItemCommand="DataList1_ItemCommand"
                Width="308px">
                <FooterStyle Font-Bold="True" Font-Italic="False" Font-Size="18pt" />
                <HeaderStyle Font-Bold="True" Font-Size="18pt" />
                <ItemStyle ForeColor="#000066" />
                <HeaderTemplate>
                    HeaderTemplate  
                    <br />
                    <hr />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="lBtnShowDetails" runat="server" CommandName="select">Details</asp:LinkButton>
                    <%#DataBinder.Eval(Container.DataItem,"FruitName") %>
                </ItemTemplate>
                <SelectedItemTemplate>
                    RowNumber:  
                    <%#DataBinder.Eval(Container.DataItem,"RowNumber") %>
                    <br />
                    FruitName:  
                    <%#DataBinder.Eval(Container.DataItem,"FruitName") %>
                    <br />
                    UnitPrice:  
                    <%#DataBinder.Eval(Container.DataItem,"UnitPrice") %>
                    <br />
                    Quantity:  
                    <%#DataBinder.Eval(Container.DataItem,"Quantity") %>
                </SelectedItemTemplate>
                <FooterTemplate>
                    <hr />
                    <br />
                    FooterTemplate
                </FooterTemplate>
            </asp:DataList>
        </form>

    code behind:

           protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    this.DataBindToDataList();
                }
            }  
            private void DataBindToDataList()
            {
                string connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString1"].ConnectionString;
                SqlConnection connection = new SqlConnection(connectionString);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = new SqlCommand("select * from Fruits", connection);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                this.DataList1.DataKeyField = "RowNumber";
                this.DataList1.DataSource = ds.Tables[0];
                this.DataList1.DataBind();
    
            }
    
            protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
            {
                if (e.CommandName == "select")
                {
                    this.DataList1.SelectedIndex = e.Item.ItemIndex;
                    this.DataBindToDataList();     
    
                }
            }

    The result of my work demo: 

     

    Best Regards,

    YongQing.

    Tuesday, May 14, 2019 5:21 AM