📄 selectpt.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SelectPT : System.Web.UI.Page
{
public SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
public string strName;
public string strID;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["username"] == null)
{
Response.Redirect("User_login.aspx?msg=" + Server.UrlEncode("SelectPT.aspx"));
}
//连续选中
if (ListBoxDateBind(ListBox1, "0"))
if (ListBoxDateBind(ListBox2, ListBox1.SelectedValue))
if (ListBoxDateBind(ListBox3, ListBox2.SelectedValue))
if (ListBoxDateBind(ListBox4, ListBox3.SelectedValue))
ListBoxDateBind(ListBox5, ListBox4.SelectedValue);
SelectName();
}
}
private bool ListBoxDateBind(ListBox ListBox, string strID)//ListBox控件的数据绑定
{
String sql = "SELECT * FROM PType where parentId=" + strID;
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader dr = command.ExecuteReader();
ListBox.DataSource = dr;
ListBox.DataValueField = "category_ID";
ListBox.DataTextField = "category_name";
ListBox.DataBind();
ListBox.SelectedIndex = 0; //默认选中第一个选项
dr.Close();
}
catch
{
return false;
}
finally
{
conn.Close();
}
return true;
}
private void SelectName()//获取选中的 类别名称和编号
{
if (ListBox1.SelectedIndex > -1)
{
strName = ListBox1.SelectedItem.Text + ">>";
strID = ListBox1.SelectedValue;
}
if (ListBox2.SelectedIndex > -1)
{
strName += ListBox2.SelectedItem.Text + ">>";
strID = ListBox2.SelectedValue;
}
if (ListBox3.SelectedIndex > -1)
{
strName += ListBox3.SelectedItem.Text + ">>";
strID = ListBox3.SelectedValue;
}
if (ListBox4.SelectedIndex > -1)
{
strName += ListBox4.SelectedItem.Text + ">>";
strID = ListBox4.SelectedValue;
}
if (ListBox5.SelectedIndex > -1)
{
strName += ListBox5.SelectedItem.Text;
strID = ListBox5.SelectedValue;
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBoxDateBind(ListBox2, ListBox1.SelectedValue))
if (ListBoxDateBind(ListBox3, ListBox2.SelectedValue))
if (ListBoxDateBind(ListBox4, ListBox3.SelectedValue))
ListBoxDateBind(ListBox5, ListBox4.SelectedValue);
SelectName();
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBoxDateBind(ListBox3, ListBox2.SelectedValue))
if (ListBoxDateBind(ListBox4, ListBox3.SelectedValue))
ListBoxDateBind(ListBox5, ListBox4.SelectedValue);
SelectName();
}
protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBoxDateBind(ListBox4, ListBox3.SelectedValue))
ListBoxDateBind(ListBox5, ListBox4.SelectedValue);
SelectName();
}
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxDateBind(ListBox5, ListBox4.SelectedValue);
SelectName();
}
protected void ListBox5_SelectedIndexChanged(object sender, EventArgs e)
{
SelectName();
}
protected void Button1_Click(object sender, EventArgs e)
{
SelectName();
Response.Redirect("product_add.aspx?categoryid=" + strID + "&categoryname=" +
Server.UrlEncode(strName));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -