Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中的类别下添加子类别_C#_Asp.net - Fatal编程技术网

C# 在代码隐藏中的类别下添加子类别

C# 在代码隐藏中的类别下添加子类别,c#,asp.net,C#,Asp.net,我有一个场景,其中我有一个类别和子类别的下拉列表。我想在前端的相应子类别下添加子类别。我已经完成了分类部分的CODE。请帮助我在相应类别下添加子类别 请查看我目前使用的代码:- <div> <asp:TextBox ID="txtCategoryAdding" runat="server"></asp:TextBox> <asp:TextBox ID="txtDescription" runat="server" TextMode="Mu

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

请查看我目前使用的代码:-

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

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

在数据库中创建一个可以作为父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 = "";
}

}

应该是相同的,只需将parentId保存在某个地方,以便在添加子项时使用它即可one@AmrElGarhy:我的表中有CategoryId作为ParentID。只需要快速开始。你能建议一下你希望它如何工作吗?你对工作流程或用户界面有什么想法吗?您希望所有内容都在同一页上还是在不同的步骤上?我希望无论何时添加类别,都应该有为相应类别添加子类别的选项。是的,我希望所有的东西都在同一页上。这在逻辑上是好的。嗨,我已经有了类别和子类别的字段。我只想添加子类别。所以使用子类别作为parentId,使用类别作为categoryOk,我会尝试让您知道