我有一个场景,我有一个类别和子类别的下拉列表。我已经为类别部分做了coede。请帮我在各自的类别下添加子类别。
请看我到目前为止的代码:
<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 = "";
}
}发布于 2014-10-29 21:34:33
您可以在div中添加一个面板并将其隐藏,然后单击insert类别按钮即可显示它。
此面板应包含要提交的子类别控件。
发布于 2014-10-29 21:35:50
在您的数据库中创建一个可以作为parent_id的字段,并记住categoryId和parentId是不同的字段,因此您的表看起来就像这个categoryId、categoryName、parentId和其他字段。
只需将parentCategory的下拉列表放在第一个textbox之前。
<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 = "";
}}
https://stackoverflow.com/questions/26629496
复制相似问题