Asp. NET implementation of the dropdown box two-level linkage components

Source: Internet
Author: User
Tags constructor dsn insert
Asp.net| pull Down

Namespace WEBAPPLICATIONDLH
{
Using System;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
Using Db;
<summary>
A summary description of area.
</summary>
public class Area:System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList ddlprovince;
protected System.Web.UI.WebControls.DropDownList ddlcity;
public string Strprovince
{
Get
{
return ddlProvince.SelectedItem.Value;
}
Set
{
DdlProvince.SelectedItem.Value = Value;
}
}
public string strcity
{
Get
{
return ddlCity.SelectedItem.Value;
}
Set
{
DdlCity.SelectedItem.Value = Value;
}
}
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Ddlprovincebind ();
DdlCity.Items.Insert (0, "-Please select City-");
}
}
private void Ddlprovincebind ()
{
Db.area Myarea = new Db.area ();
SqlDataReader dr = Myarea.getallprovince ();
if (Dr. Read ())
{
Ddlprovince.datasource = Dr;
Ddlprovince.datatextfield = "province";
Ddlprovince.datavaluefield = "province";
Ddlprovince.databind ();
DdlProvince.Items.Insert (0, "-Please select Province-");
}
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

///<summary>
///The designer supports the method you want-do not modify the contents of this method using the Code Editor
///.
///</summary>
private void InitializeComponent ()
{
This.ddlProvince.SelectedIndexChanged + = new System.EventHandler (this.ddlprovince_selectedindexchanged);
this. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
private void Ddlprovince_selectedindexchanged (object sender, System.EventArgs e)
{
if ( DdlProvince.SelectedItem.Value = = "-Please select Province-")
{
DdlCity.Items.Clear ();
DdlCity.Items.Insert (Ddlprovince.selectedindex, "-Please select City-");
}
Else
{
Db.area myarea = new Db.area ();
SqlDataReader dr = Myarea.getallcitywhereprovince (DdlProvince.SelectedItem.Value);
if (Dr. Read ())
{
Ddlcity.datasource = dr;
Ddlcity.datatextfield = "City";
Ddlcity.datavaluefield = "ID";
Ddlcity.databind ();
}
}
}
}
}

=============================================================〉
Using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; A summary description of the
namespace Db
{
///<summary>
///area.
///</summary>
public class Area:base
{
Public area ()
{
//
//TODO: Add constructor logic here
// br>}
Public SqlDataReader getallprovince ()
{
Strsp = "Sp_area_selectall";
Drselectall (STRSP);
Return Dr;
}
Public SqlDataReader getallcitywhereprovince (string a)
{
conn = new SqlConnection ( configurationsettings.appsettings["DSN"]);
cmd = new SqlCommand ("Sp_area_selectallwhereprovince", conn);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add ("@Province", sqldbtype.varchar,50). Value = A.tostring ();
Conn. Open ();
Dr = cmd. ExecuteReader (commandbehavior.closeconnection);
Return Dr;
}
}
}

=====================================================〉
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Sp_area_selectall] and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)
drop procedure [dbo]. [Sp_area_selectall]
Go
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Sp_area_selectallwhereprovince] and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)
drop procedure [dbo]. [Sp_area_selectallwhereprovince]
Go
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Area] ') and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [Area]
Go
CREATE TABLE [dbo]. [Area] (
[ID] [int] not NULL,
[Province] [varchar] (m) COLLATE chinese_prc_ci_as NULL,
[City] [varchar] (m) COLLATE chinese_prc_ci_as NULL,
Other [varchar] (m) COLLATE chinese_prc_ci_as NULL
) on [PRIMARY]
Go
SET QUOTED_IDENTIFIER ON
Go
SET ANSI_NULLS on
Go
CREATE PROCEDURE Sp_area_selectall
As
SELECT DISTINCT Province
From area
Order by province
Return
Go
SET QUOTED_IDENTIFIER OFF
Go
SET ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER ON
Go
SET ANSI_NULLS on
Go
CREATE PROCEDURE sp_area_selectallwhereprovince
@Province varchar (50)
As
SELECT area.*
From area
WHERE (province = @Province)
Return
Go
SET QUOTED_IDENTIFIER OFF
Go
SET ANSI_NULLS on
Go
================================================〉
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration; Namespace Db
{
<summary>
A summary description of Base.
</summary>
public class Base
{
Public Base ()
{
//
TODO: Add constructor logic here
//
}
protected string con = configurationsettings.appsettings["DSN"];
Protected SqlConnection conn;
protected SqlCommand cmd;
protected SqlDataReader Dr;
protected string strsp;
Protected SqlDataReader Drselectall (string strsp)
{
conn = new SqlConnection (Con);
cmd = new SqlCommand (strsp,conn);
Cmd.commandtype = CommandType.StoredProcedure; Conn. Open ();
Dr = cmd. ExecuteReader (commandbehavior.closeconnection);
Return Dr;
}//returns a SqlDataReader}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.