locked
Asp.Net c# Help with button and repeater RRS feed

  • Question

  • User-1473429353 posted

    I have a c# web form and I need help with connecting a button outside of a repeater with the controls in the repeater.

    I have tried a few different ways but I'm failing to grasp how to do it. When the button is clicked, only the cars with the make and model that have been selected in the drop down lists should be displayed. But upon the page loading, all cars in the database should be displayed.  

    Here is my code for the default.aspx file:

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FaizaCarsWebsite._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
        <asp:Repeater runat="server" ID="repeater1">
            <HeaderTemplate>
                <div class="row">
            </HeaderTemplate>
            <ItemTemplate>
                <div class="col-12 col-sm-6 col-md-4 col-lg-2">
                    <a href="/Stock.aspx?StockID=<%#Eval("Stock_ID")%>">
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("data:image/png;base64,{0}", Convert.ToBase64String((byte[])Eval("Picture"))) %>' Width="200px" />
                    </a>
                    <asp:Label ID="LblMake" runat="server" Text='<%#Eval("Make") %>'> </asp:Label>
                    <asp:Label ID="LblModel" runat="server" Text='<%#Eval("Model") %>'> </asp:Label>
                    <asp:Label ID="LblMileage" runat="server" Text='<%#Eval("Mileage") %>'> </asp:Label>
                </div>
    
            </ItemTemplate>
            <FooterTemplate>
                </div>
            </FooterTemplate>
        </asp:Repeater>
        <div class="row">
            <asp:DropDownList ID="DDLMakes" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDLMakes_SelectedIndexChanged"></asp:DropDownList>
            <asp:DropDownList ID="DDLModels" runat="server"></asp:DropDownList>
        </div>
        <div class="row">
            <asp:Button ID="btnFilter" runat="server" Text="Filter" OnClick="btnFilter_Click" />
        </div>
    
    
    </asp:Content>

    Here is my code for the default.aspx.cs file:

       public partial class _Default : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    repeater1.DataSource = Getcars();
                    repeater1.DataBind();
                    DDLMakes.DataSource = GetMakes();
                    DDLMakes_SelectedIndexChanged(null, null);
                }
    
            }
    
    
            private DataTable Getcars()
            {
                DataTable dt = new DataTable();
    
                try
                {
                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCore"].ConnectionString.ToString()))
                    {
                        string SQL = "SELECT tblStock.Stock_ID,tblMake.Make, tblModel.Model, tblStock.Mileage, tblStock.Picture FROM((tblStock INNER JOIN tblMake ON tblStock.Make_ID = tblMake.Make_ID) INNER JOIN tblModel ON tblStock.Model_ID = tblModel.Model_ID) WHERE tblStock.Picture is not NULL ";
                        SqlCommand myCommand = new SqlCommand(SQL, cn);
                        SqlDataAdapter da = new SqlDataAdapter() { SelectCommand = myCommand };
                        da.Fill(dt);
                        da.Dispose();
                    }
                }
                catch (Exception ex)
                {
    
                }
    
                return dt;
            }
    
    
            private DataTable GetMakes()
            {
                DataTable dt = new DataTable();
                try
                {
                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCore"].ConnectionString.ToString()))
                    {
                        string SQL = "SELECT * from tblMake";
                        SqlCommand command = new SqlCommand(SQL, cn);
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = command;
                        da.Fill(dt);
                        da.Dispose();
                        DDLMakes.DataBind();
                        DDLMakes.DataSource = dt;
                        DDLMakes.DataTextField = "Make";
                        DDLMakes.DataValueField = "Make_ID";
                        DDLMakes.DataBind();
    
                    }
                }
    
                catch (Exception ex)
                {
    
                }
                return dt;
    
            }
    
            private DataTable GetModels(int MakeID)
            {
                DataTable dt = new DataTable();
                try
                {
                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCore"].ConnectionString.ToString()))
                    {
                        string SQL = "SELECT * from tblModel WHERE Make_ID = @MakeID";
                        SqlCommand command = new SqlCommand(SQL, cn);
                        command.Parameters.Add(new SqlParameter("@MakeID", MakeID));
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = command;
                        da.Fill(dt);
                        da.Dispose();
    
                    }
                }
    
                catch (Exception ex)
                {
    
                }
                return dt;
            }
    
    
            protected void DDLMakes_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DDLMakes.SelectedIndex == -1)
                {
                    DDLModels.Items.Clear();
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(DDLMakes.SelectedValue.GetType());
                    if (DDLMakes.SelectedValue.GetType() == typeof(string))
                    {
                        DataTable Models = GetModels(Convert.ToInt32(DDLMakes.SelectedValue));
                        DDLModels.DataSource = Models;
                        DDLModels.DataBind();
                        DDLModels.DataTextField = "Model";
                        DDLModels.DataValueField = "Model_ID";
                        DDLModels.DataBind();
                    }
                }
            }
    
    
            private DataTable GetFilterData()
            {
                DataTable dt = new DataTable();
                try
                {
                    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbCore"].ConnectionString.ToString()))
                    {
                        string SQL = "SELECT tblStock.Stock_ID,tblMake.Make, tblModel.Model, tblStock.Mileage, tblStock.Picture FROM((tblStock INNER JOIN tblMake ON tblStock.Make_ID = tblMake.Make_ID) INNER JOIN tblModel ON tblStock.Model_ID = tblModel.Model_ID) WHERE tblStock.Make_ID= '" + DDLMakes.Text + "', tblStock.Model_ID='" + DDLModels.Text + "'  ";
                        SqlCommand command = new SqlCommand(SQL, cn);
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = command;
                        da.Fill(dt);
                        da.Dispose();
                    }
    
                }
                catch (Exception ex)
                {
    
                }
    
                return dt;
            }
    
            protected void btnFilter_Click(object sender, EventArgs e)
            {
                repeater1.DataSource = GetFilterData();
                repeater1.DataBind();
            }
    
    
            protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
    
            }

    I'm not sure what to insert into the repeater_ItemDataBound function to change the values of the labels. 

    Friday, September 25, 2020 12:09 PM

Answers

  • User409696431 posted

    If you want a button click to change what is shown in the repeater, in your button click code, get the values from the dropdownlist and rebind the repeater with a new SELECT statement that includes a WHERE statement filtering for the cars you want.

    I am not sure what you are asking here: "I'm not sure what to insert into the repeater_ItemDataBound function to change the values of the labels."

    You don't do anything in ItemDataBound.  You do as I suggested above: rebind the repeater with the data you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 25, 2020 3:13 PM