简体   繁体   中英

How do I get a ListBox to display all info for an "ID" from a Textbox (ASP.NET C#)

I'm working on a project where I want to display all of the incident information (from Database) for a Customer (which I get from a textbox/ user input).

I'm struggling to fill the ListBox and am not sure that I have the right idea so far for using the "RowFilter" function.

public partial class CustomerSurvey : System.Web.UI.Page
{
    private Incident incidentInformation;
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnGet_Incidents(object sender, EventArgs e)
    {
        incidentInformation = this.GetInfoForSelectedCustomerID();
    }

    private Incident GetInfoForSelectedCustomerID()
    {
        //get row from AccessDataSource based on value in dropdownlist
        DataView customersTable = (DataView)
            SqlDataSource2.Select(DataSourceSelectArguments.Empty);
        customersTable.RowFilter =
            "CustomerID = '" + txtCustomerID_ForIncidents + "'";
        DataRowView row = (DataRowView)customersTable[0];

        //create a new product object and load with data from row
        Incident i = new Incident();

        i.IncidentID = (int)row["IncidentID"];
        i.CustomerID = (int)row["CustomerID"];
        i.ProductCode = row["ProductCode"].ToString();
        i.TechID = (int)row["TechID"];
        i.DateOpened = (DateTime)row["DateOpened"];
        i.DateClosed = (DateTime)row["DateClosed"];
        i.Title = row["Title"].ToString();
        i.Description = row["Description"].ToString();

        return i;
    }

    private void DisplayInfo()
    {
        Incident incident = (Incident)Session["Reservation"];

        lstCustomers.Display
    }

As you can see above the function DisplayInfo is unfinished. This is because I don't know how to fill the ListBox.

Another thing that I am considering is that there might be multiple incidents to a customer and I am not sure that I have the right approach with the class.

Thanks!

Do you want to display the data in the database based on the Textbox value in the ListBox? If so, you can refer to below code. If I misunderstand your requirement, please post more details information about your requirement.

Aspx,

 <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> CustomerId:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnGet_Incidents" /> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ThreadDemoConnectionString %>" SelectCommand="SELECT [CustomerId], [Name], [Country] FROM [Customers]"></asp:SqlDataSource>

Aspx.cs:

protected void btnGet_Incidents(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    GetInfoForSelectedCustomerID();
}

private void GetInfoForSelectedCustomerID()
{

    //get row from AccessDataSource based on value in dropdownlist
    DataView customersTable = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
    customersTable.RowFilter = "CustomerId = '" + TextBox1.Text + "'";
    DataRowView row = (DataRowView)customersTable[0];

    ListBox1.Items.Add(row["CustomerId"].ToString());
    ListBox1.Items.Add(row["Name"].ToString());
    ListBox1.Items.Add(row["Country"].ToString());

}

The result,

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM