【问题标题】:How do I get a ListBox to display all info for an "ID" from a Textbox (ASP.NET C#)如何让 ListBox 显示来自文本框的“ID”的所有信息(ASP.NET C#)
【发布时间】:2019-12-11 00:50:16
【问题描述】:

我正在处理一个项目,我想在其中显示客户的所有事件信息(来自数据库)(我从文本框/用户输入中获取)。

我正在努力填充 ListBox,但我不确定到目前为止我是否有正确的想法来使用“RowFilter”函数。

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
    }

正如您在上面看到的,函数 DisplayInfo 尚未完成。这是因为我不知道如何填充 ListBox。

我正在考虑的另一件事是,客户可能会遇到多起事件,我不确定我在课堂上的方法是否正确。

谢谢!

【问题讨论】:

    标签: c# asp.net .net visual-studio


    【解决方案1】:

    是否要根据 ListBox 中的 Textbox 值显示数据库中的数据?如果是这样,您可以参考下面的代码。 如果我误解了您的要求,请发布有关您的要求的更多详细信息。

    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());
    
    }
    

    结果,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 2023-03-26
      • 1970-01-01
      • 2015-08-25
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多