无法在ASP.NET

时间:2016-01-31 15:40:02

标签: c# asp.net .net webforms

我需要根据所选的子类别获取产品。现在,它显示所有产品。这该怎么做?这是我的代码。如何在按钮单击中传递Subcategory.Id?

...
<td>Subcategory</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Id">
    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource2"
         runat="server"
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
         SelectCommand="SELECT [Id], [Name] FROM [SubCategory] WHERE ([IdCategory] = @IdCategory)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="IdCategory" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

</td>
</tr>
<tr>
<td>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
</tr>
...



protected void Button1_Click(object sender, EventArgs e)
{
...
        SqlCommand command = new SqlCommand("SELECT productName, quantity, price FROM Product JOIN SubCategory ON Product.id_subcategory = SubCategory.id", _connection);
...

}

2 个答案:

答案 0 :(得分:0)

假设您具有以下数据库表结构:

Product
- Id (PK)
- ProductName
- Quantity
- Price
- MainCatID (FK)
- SubCatID (FK)
- others...

Category
- Id (PK)
- Name

SubCategory
- Id (PK)
- Name
- IdCategory (FK)
  1. 确保添加名称空间System.Configuration,以便您可以从web.config文件访问连接字符串。
  2. 实例化SqlConnection类以标识数据库连接:
  3. 创建方法以显示各自下拉列表控件中的类别和子类别列表
  4. 向DropDownList1添加AutoPostBack属性,然后设置为true,这样每次从列表中选择一个项目时,它都会重新生成&#39;基于所选类别值的子类别列表。
  5. 创建DropDownList1的OnSelectedIndexChanged事件,调用DisplaySubCategories()方法。
  6. 创建一个方法,根据所选类别和子类别值显示产品列表
  7. 包括从步骤#4到DropDownList2的类似过程
  8. 在页面加载事件中调用两个方法。
  9. SqlConnection con = new SqlConnection(ConfigurationManager.
        ConnectionStrings["ConnectionString"].ConnectionString);
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayCategories();
            DisplaySubCategories();
        }
    }
    
    void DisplayCategories()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT Id, Name FROM Category";
        SqlDataReader data = cmd.ExecuteReader();
        DropDownList1.DataSource = data;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataBind();
        con.Close();
    }
    
    void DisplaySubCategories(string ID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT Id, Name FROM SubCategory WHERE IdCategory = @IdCategory";
        cmd.Parameters.AddWithValue("@IdCategory", ID);
        SqlDataReader data = cmd.ExecuteReader();
        DropDownList2.DataSource = data;
        DropDownList2.DataTextField = "Name";
        DropDownList2.DataValueField = "Id";
        DropDownList2.DataBind();
        con.Close();
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DisplaySubCategories(DropDownList2.SelectedValue);
    }
    
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DisplayProducts(DropDownList1.SelectedValue, DropDownList2.SelectedValue);
    }
    
    void DisplayProducts(string mainCatID, string subCatID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = @"SELECT productName, quantity, price FROM Product 
            WHERE MainCatID = @MainCatID AND SubCatID=@SubCatID";
        cmd.Parameters.AddWithValue("@MainCatID", mainCatID);
        cmd.Parameters.AddWithValue("@SubCatID", subCatID);
        SqlDataReader data = cmd.ExecuteReader();
        string result = string.Empty;
        while (data.Read())
        { 
            result += "Name = " + Convert.ToString(reader["productName"]) + "; ";
            result += "Quantity = " + Convert.ToString(reader["quantity"]) + "; ";
            result += "Price = " + Convert.ToString(reader["price"]);
            result += "<br />";
        }
        ReadAllOutput.Text = result;
        con.Close();
    }
    

    <强>的.aspx

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" />
    

    我删除了所有现有的SqlDataSource控件,因为我发现它们很乱。

    更新

    您还可以在Button click事件

    中声明DisplayProducts()方法
    protected void Button1_Click(object sender, EventArgs e)
    {
        DisplayProducts(DropDownList1.SelectedValue, DropDownList2.SelectedValue);
    }
    

    PS:某些语法可能不正确(区分大小写),而且我目前还没有使用IDE。

答案 1 :(得分:0)

我假设DropDownList2控件已经包含子类别数据。

您可以使用

在Button1_Click事件中获取子类别ID
DropDownList2.SelectedValue