|
private DataTable Get_Dt(string sql)
{
String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection=new SqlConnection(strConnection);
myConnection.Open();
SqlDataAdapter myAdp=new SqlDataAdapter(sql,myConnection);
DataTable myDt = new DataTable();
try
{
//Data input
myAdp.Fill(myDt);
//Return data set
return(myDt);
}
catch(OleDbException ex)
{
//Display error message
throw ex;
}
finally
{
//Close the database connection
myConnection.Close();
}
}
/// <summary>
/// page load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Load(object sender, System.EventArgs e)
{
//Receive parameters
string sortid = this.Request.QueryString["sortid"];
//Determine whether the parameter is empty (Note: There are two forms of empty, one is null and one is empty)
if(sortid + "a" != "a")
{
//If the above parameters are passed, it means that the linkage operation starts
this.xmlBind(sortid);
}
//When the page is first loaded, bind the first/second drop-down box, and the second drop-down box is all values
// But in fact, the second drop-down box should display empty values, because all the values may be quite a lot, it is best to display only a "please select"
if(!this.IsPostBack)
{
this.DownBind1();
this.DownBind2();
}
}
/// <summary>
/// Return the required value of the second drop-down box to xmlhttp
/// </summary>
/// <param name="sortid">Key ID value passed</param>
private void xmlBind(string sortid)
{
string mystr = "";
string sql = "select typename,typeid from class_2 where sortid = "+ sortid;
DataTable mytab = this.Get_Dt(sql);
//Form the obtained values: ID|Name, ID|Name...
if(mytab.Rows.Count != 0)
{
for(int i=0;i<mytab.Rows.Count;i++)
{
mystr += "," + mytab.Rows[i]["typeid"].ToString() + "|" + mytab.Rows[i]["typename"].ToString();
}
mystr = mystr.Substring(1);
}
//Output page
this.Response.Write(mystr);
this.Response.End();
}
/// <summary>
/// Bind the first drop-down box
/// </summary>
private void DownBind1()
{
//Show all main categories
string sql = "select sortid,sort from class_1 order by sortid asc ";
DataTable mytab = this.Get_Dt(sql);
//Bind the first drop-down box
this.mydown1.DataSource = mytab;
this.mydown1.DataValueField = "sortid";
this.mydown1.DataTextField = "sort";
this.mydown1.DataBind();
//Add a "Please select" line
this.mydown1.Items.Insert(0,new ListItem("Please select category",""));
//Add a default selection for this drop-down box (here default is sortid = 1
//When making an option, if you add the selected item and there is no item in this control, an error will occur
//For example: this.mydown1.SelectedValue = "1";
//So here is selected as follows
ListItem myItem = this.mydown1.Items.FindByValue("1");
if(myItem != null)
{
myItem.Selected = true;
}
//Add a selection event for this drop-down box, the first parameter is yourself, and the second parameter is the name of the drop-down box to be filled
this.mydown1.Attributes.Add("onchange","XmlPost(this,'" + this.mydown2.ClientID + "');");
}
The
/// <summary>
/// Bind the second drop-down box
/// </summary>
private void DownBind2()
{
//The default is to display all subcategories with classification number 1
string sql = "select sortid,typename,typeid from class_2 where sortid =1";
DataTable mytab = this.Get_Dt(sql);
//Fix the control
this.mydown2.DataSource = mytab;
this.mydown2.DataSource = mytab;
this.mydown2.DataValueField = "typeid";
this.mydown2.DataTextField = "typename";
this.mydown2.DataBind();
//Add an empty first line
this.mydown2.Items.Insert(0,new ListItem("Please choose a name",""));
} |
|