. Create a new provincecity user control. Add a Button1 button to the control.
Reference the Provinccity control in the Demo1.aspx page. and add button1 and Lable1 controls.
When you click the button in Demo1, the province and city selected by the user in the Provincecity control are displayed in Lable1.
Using two methods, when clicking on the button1 in the user control, let the lable in the Demo1 display the content selected in the DrowpdownList2 in the Provincecity control.
Master Page Front Desk:
<%@ control xlanguage= "C #" autoeventwireup= "true" codebehind= "SelectCity.ascx.cs" inherits= "_12_25.selectcity"% >
Province: <asp:dropdownlist id= "DropDownList1"
runat= "Server" autopostback= "True" height= "20px"
Xonselectedindexchanged= "dropdownlist1_selectedindexchanged" width= "126px" >
</asp:DropDownList>
City: <asp:dropdownlist id= "DropDownList2" runat= "Server" height= "20px"
Width= "126PX" >
</asp:DropDownList>
<p>
</p>
<asp:button id= "Button1" runat= "Server" xonclick= "Button1_Click"
Text= "Get value from label in page"/>
Master Page Background:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Configuration;
Using System.Data.SqlClient;
Using System.Data;
Namespace _12_25
{
public partial class SelectCity:System.Web.UI.UserControl
{
Public event Cityselecthandler getcityselect;//defines a corresponding type of event based on a delegate
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Dropdownlistbind ();
}
}
private void Dropdownlistbind ()
{
String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (Constr))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd.commandtext = "Select Provinceid,province from Province";
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
DataTable dt = new DataTable ();
Adapter. Fill (DT);
Dropdownlist1.datasource = DT;
Dropdownlist1.datatextfield = "province";
Dropdownlist1.datavaluefield = "Provinceid";
Dropdownlist1.databind ();
}
}
}
public string Returnddl
{
Get {return "you selected is:" + DropDownList1.SelectedItem.Text + "province" + DropDownList2.SelectedItem.Text + "city";}
}
protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
DropDownList2.Items.Clear ();
String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (Constr))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
String str = this. Dropdownlist1.selectedvalue;
Cmd.commandtext = "Select Cityid,city from the city where father= @id";
Cmd. Parameters.Add (New SqlParameter ("id", str));
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
DataTable dt = new DataTable ();
Adapter. Fill (DT);
Dropdownlist2.datasource = DT;
Dropdownlist2.datatextfield = "City";
Dropdownlist2.datavaluefield = "Cityid";
Dropdownlist2.databind ();
}
}
}
protected void Button1_Click (object sender, EventArgs e)
{
Label Lab = this. Parent.findcontrol ("Label1") as Label;
Lab. Text = "You choose the provinces and cities as:" + dropdownlist1.selecteditem.text+ "" +dropdownlist2.selecteditem.text;
Fires getcityselect when the user clicks Button1
if (this. DropDownList2.SelectedValue.ToString ()!= "")
{
Getcityselect (This,dropdownlist2.selecteditem.text);
}
}
}
public delegate void Cityselecthandler (object sender,string selectcity); Add a delegate
}
DEMO1 Front Desk:
<form id= "Form1" runat= "Server" >
<div>
<uc1:selectcity id= "selectCity1" runat= "Server"/>
<br/><br/><br/><br/>
<asp:button id= "btnshowcity" runat= "Server" xonclick= "Btnshowcity_click"
Text= "Show City"/>
<asp:label id= "Label1" runat= "Server" ></asp:Label>
</div>
</form>
Demo1 Backstage
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace _12_25
{
public partial class Demo1:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Increasing the event handlers for user control Cityselect.ascx
This.selectCity1.GetCitySelect + = new Cityselecthandler (selectcity1_getcityselect);
}
void Selectcity1_getcityselect (object sender, String selectcity)//Press the TAB key to instantiate the delegate and generate a method
{
This. Label1.Text = selectcity;
}
protected void Btnshowcity_click (object sender, EventArgs e)
{
#region Method 1 to find DropDownList in FindControl in a custom control
DropDownList DDL = This.selectCity1.FindControl ("DropDownList1") as DropDownList;
DropDownList DDL1 = This.selectCity1.FindControl ("DropDownList2") as DropDownList;
Label1.Text = "Your choice is:" +ddl. selecteditem.text+ "province" +ddl1. selecteditem.text+ "City";
#endregion
#region Custom properties in the master page
Label1.Text = this.selectCity1.returnDDl;
#endregion
}
}
}
4 new demo2.aspx, reference provincecity, when clicked Button in Provincecity, show selected province and city in the page.
Master Page Front Desk:
<%@ control xlanguage= "C #" autoeventwireup= "true" codebehind= "SelectCity.ascx.cs" inherits= "_12_25.selectcity"% >
Province: <asp:dropdownlist id= "DropDownList1"
runat= "Server" autopostback= "True" height= "20px"
Xonselectedindexchanged= "dropdownlist1_selectedindexchanged" width= "126px" >
</asp:DropDownList>
City: <asp:dropdownlist id= "DropDownList2" runat= "Server" height= "20px"
Width= "126PX" >
</asp:DropDownList>
<p>
</p>
<asp:button id= "Button1" runat= "Server" xonclick= "Button1_Click"
Text= "Get value from label in page"/>
Master Page Background:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Configuration;
Using System.Data.SqlClient;
Using System.Data;
Namespace _12_25
{
public partial class SelectCity:System.Web.UI.UserControl
{
Public event Cityselecthandler getcityselect;//defines a corresponding type of event based on a delegate
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Dropdownlistbind ();
}
}
private void Dropdownlistbind ()
{
String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (Constr))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
Cmd.commandtext = "Select Provinceid,province from Province";
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
DataTable dt = new DataTable ();
Adapter. Fill (DT);
Dropdownlist1.datasource = DT;
Dropdownlist1.datatextfield = "province";
Dropdownlist1.datavaluefield = "Provinceid";
Dropdownlist1.databind ();
}
}
}
public string Returnddl
{
Get {return "you selected is:" + DropDownList1.SelectedItem.Text + "province" + DropDownList2.SelectedItem.Text + "city";}
}
protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
DropDownList2.Items.Clear ();
String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (Constr))
{
Conn. Open ();
using (SqlCommand cmd = conn. CreateCommand ())
{
String str = this. Dropdownlist1.selectedvalue;
Cmd.commandtext = "Select Cityid,city from the city where father= @id";
Cmd. Parameters.Add (New SqlParameter ("id", str));
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
DataTable dt = new DataTable ();
Adapter. Fill (DT);
Dropdownlist2.datasource = DT;
Dropdownlist2.datatextfield = "City";
Dropdownlist2.datavaluefield = "Cityid";
Dropdownlist2.databind ();
}
}
}
protected void Button1_Click (object sender, EventArgs e)
{
if (this. DropDownList2.SelectedValue.ToString ()!= "")
{
String str = DropDownList1.SelectedItem.Text + " " + DropDownList2.SelectedItem.Text;
Getcityselect (THIS,STR);
}
}
}
public delegate void Cityselecthandler (object sender,string selectcity); Add a delegate
}
Demo2 Front Desk:
<form id= "Form1" runat= "Server" >
<div>
<uc1:selectcity id= "selectCity1" runat= "Server"/>
<asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
</div>
</form>
Demo2 background:
protected void Page_Load (object sender, EventArgs e)
{
Increasing the event handlers for user control Cityselect.ascx
This.selectCity1.GetCitySelect + = new Cityselecthandler (selectcity1_getcityselect);
}
Void Selectcity1_getcityselect (object sender, String selectcity)//Press the TAB key to instantiate the delegate and generate a method
{
this. Label1.Text = selectcity;
}