用户控件的运用和读取控件的数据

 
N 方法一:
使用用户控件的FindControl方法,找到用户控件中的控件,进一步取得其数据(不需要在控件中处理,但是需要知道用户控件中要使用的控件的ID)
在用户控件中使用Parent属性找到页面,再通过FindControl方法找到要访问的控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class citySelect : System.Web.UI.UserControl
{
public event CitySelectHandler OnCitySelect;
/// <summary>
/// 获取用户选择的
/// </summary>
public string ProvinceCity
{
get
{
return this.DropDownList1.SelectedItem.Text +
"(" +
this.DropDownList1.SelectedValue +
");" +
this.DropDownList2.SelectedItem.Text +
"(" +
this.DropDownList2.SelectedValue +
")";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindProvince();
}
private void bindProvince(){
string str = System.Configuration.ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
using(SqlConnection sqlcnn = new SqlConnection(str)){
using(SqlCommand sqlcmm = sqlcnn.CreateCommand()){
sqlcmm.CommandText = "select provinceid,province from province";
sqlcnn.Open();
DropDownList1.DataSource = sqlcmm.ExecuteReader();
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceid";
DropDownList1.DataBind();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){
this.DropDownList2.Items.Clear();
string pid = this.DropDownList1.SelectedValue;
string str = System.Configuration.ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str)){
using (SqlCommand sqlcmm = sqlcnn.CreateCommand()){
sqlcmm.CommandText = "select cityid,city from city where father='" + pid + "'";
sqlcnn.Open();
DropDownList2.DataSource = sqlcmm.ExecuteReader();
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "cityid";
DropDownList2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Label lbl = this.Parent.FindControl("Label1") as Label;
//lbl.Text = this.DropDownList2.SelectedItem.Text;
if (OnCitySelect != null)
{
OnCitySelect(this, this.DropDownList2.SelectedItem.Text);
}
}
}
public delegate void CitySelectHandler(object sender,string selectCity);

页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.city1.OnCitySelect +=
new CitySelectHandler(city1_OnCitySelect);
}

void city1_OnCitySelect(object sender, string selectCity)
{
this.Label1.Text = selectCity;
}

protected void Button1_Click(object sender, EventArgs e)
{
#region 方法一
DropDownList ddl = this.city1.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
this.Label1.Text = "省份:" + ddl.SelectedItem.Text + "(" + ddl.SelectedValue + ");";
ddl = this.city1.FindControl("DropDownList2") as DropDownList;
if (ddl != null)
this.Label1.Text += "城市:" + ddl.SelectedItem.Text + "(" + ddl.SelectedValue + ");";
#endregion

this.Label1.Text = this.city1.ProvinceCity;
}
}

N 方法二:
为控件增加属性(方法),使用该属性访问用户控件中的数据(在目标页面中使用时,简单,但是不够灵活)
为用户控件添加事件,在页面中处理事件,进而实现在控件中访问页面中的控件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值