简体   繁体   English

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

[英]Adding Sub category under category in code behind

I have a scenario where I have a dropdowns for category and sub category. 我有一个场景,其中有一个类别和子类别的下拉列表。 I want to add the sub categoy under the respective sub category from the front end. 我想从前端在相应子类别下添加子类别。 I have done the coede for category section. 我已经完成了“类别”部分的工作。 Please help me with to add the sub-category under the respective category. 请帮助我在相应类别下添加子类别。

Please see the code so far which I did:- 请查看到目前为止我所做的代码:-

<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>

Button click code for adding the category:- 按钮单击代码以添加类别:-

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 = "";
    }
}

You can add a panel inside the div and make it hidden, and on your insert category button click you can show it. 您可以在div内添加一个面板并将其隐藏,然后在插入类别按钮上单击以显示它。
This panel should contain the sub category controls to submit. 此面板应包含要提交的子类别控件。

create a field in your database able as parent_id and remember categoryId and parentId are different fields so your table look like this categoryId categoryName parentId and others. 在您的数据库中创建一个可以用作parent_id的字段,并记住categoryId和parentId是不同的字段,因此您的表看起来像这个categoryId categoryName parentId和其他字段。

just put a dropdownlist for parentCategory before first textbox . 只需在第一个文本框之前放置一个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();

} }

after submit button 提交按钮后

    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 = "";
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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