Master Page Practice

Source: Internet
Author: User
Tags connectionstrings
. 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>
&nbsp; City: <asp:dropdownlist id= "DropDownList2" runat= "Server" height= "20px"
Width= "126PX" >
</asp:DropDownList>
<p>
&nbsp;</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"/>
&nbsp;
<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>
&nbsp; City: <asp:dropdownlist id= "DropDownList2" runat= "Server" height= "20px"
Width= "126PX" >
</asp:DropDownList>
<p>
&nbsp;</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 + "&nbsp;&nbsp;&nbsp;" + 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;
}

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.