在代码后面的类别下添加子类别

时间:2014-10-29 11:45:28

标签: c# asp.net

我有一个场景,我有类别和子类别的下拉列表。我想在前端添加相应子类别下的子类别。我已经完成了类别部分的同步。请帮我添加相应类别下的子类别。

请查看我到目前为止的代码: -

<div>
    <asp:TextBox ID="txtCategoryAdding" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine"></asp:TextBox><br />
    <br />
    <asp:Button ID="btnAdd" Text="Add Category" Width="100" runat="server" OnClick="btnAdd_Click" />
</div>

按钮点击代码以添加类别: -

protected void btnAdd_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection())
    {
        string query;
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
        conn.Open();
        query = "Insert into Categories_For_Merchant values ('" + txtCategoryAdding.Text + "', '" + txtDescription.Text + "')";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Write("<script>alert('Category added succesfully');</script>");
        txtCategoryAdding.Text = "";
        txtDescription.Text = "";
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在div中添加一个面板并将其隐藏,然后在插入类别按钮上单击即可显示它 此面板应包含要提交的子类别控件。

答案 1 :(得分:0)

在数据库中创建一个能够作为parent_id的字段,并记住categoryId和parentId是不同的字段 所以你的桌子看起来像这样        categoryId categoryName parentId和其他。

在第一个文本框之前为parentCategory添加一个下拉列表。

  <div>
    <asp:DropDownList ID="ddl_parent" runat="server" Width="160px" ></asp:DropDownList>
    <asp:TextBox ID="txtCategoryAdding" runat="server"></asp:TextBox>
    <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine"></asp:TextBox>
    <br />
    <asp:Button ID="btnAdd" Text="Add Category" Width="100" runat="server" OnClick="btnAdd_Click"
    />
 </div>


   on page_load event on .cs page

  protected void Page_Load(object sender, EventArgs e)
      {

       SqlConnection conn = new  SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
    conn.Open();  
        SqlDataAdapter ad1 = new SqlDataAdapter();
        ad1.SelectCommand = new SqlCommand("select categoryID,categoryName from  
                   Categories_For_Merchant where parent_id=0",conn);
        ds = new DataSet();
        ad1.Fill(ds,"parent_cat");


        ddl_parent.DataSource = ds.Tables["parent_cat"];
        ddl_parent.DataTextField = "categoryID";
        ddl_parent.DataValueField = "categoryName ";
        ddl_parent.DataBind();
        ddl_parent.Items.Insert(0, new ListItem("Select", "0"));
        conn.close();

}

提交按钮后

    protected void btnAdd_Click(object sender, EventArgs e)
  {
   using (SqlConnection con = new SqlConnection())
   {
    string query;
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultSQLConnectionString"].ConnectionString);
    conn.Open();
    query = "Insert into Categories_For_Merchant values (ddl_category.selectedItem.value,'" + txtCategoryAdding.Text + "', '" + txtDescription.Text + "')";
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    Response.Write("<script>alert('Category added succesfully');</script>");
    txtCategoryAdding.Text = "";
    txtDescription.Text = "";
}

}