自定义控件传值
我们知道,如果传一个不固定的值给自定义控件是比较困难的,以下方法希望能大家有所帮助。思路很简单,就是定义一个公共方法,然后在使用它的页面调用它。现在页面上有两个下拉框,drp1,drp2
前面很简单,现在看我们在页面中怎样子调用前面的这个自定义控件了。
1//UserControl1.aspx
2<%@ Control Language="c#" AutoEventWireup="false" Codebehind="UserControl1.ascx.cs" Inherits="Test.UserControl1.ascx.cs" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
3<asp:DropDownList id="drp1" AutoPostBack="True" runat="server"></asp:DropDownList>
4<asp:DropDownList id="drp2" AutoPostBack="True" runat="server"></asp:DropDownList>
5
6//UserControl1.ascx.cs
7using System;
8using System.Collections;
9using System.Data;
10using System.Web.UI;
11using System.Web.UI.WebControls;
12
13using namespace Test
14public class UserContorl1:UserControl
15{
16protected DropDownList drp1;
17protected DropDownList drp2;
18
19public void OnInit(int num1,int num2)
20{
21 BindDrp1(num1);
22 BindDrp2(num2);
23}
24//绑定drp1
25private void BindDrp1(int num)
26{
27 ArrayList al=new ArrayList();
28 ListItem lst;
29 for(int i=1;i<=num;i++)
30 {
31 lst=new ListItem("第"+i.ToString()+"轮",i.ToString());
32 al.Add(lst);
33 }
34 drp1.DataSource=al;
drp1.DataTextField="Text";
drp1.DataValueField="Value";
35 drp1.DataBind();
drp1.SelectedValue=num.ToString();
36}
37//绑定drp2
38private void BindDrp2(int num)
39{
40 ArrayList al=new ArrayList();
41 ListItem lst;
42 for(int i=1;i<=num;i++)
43 {
44 lst=new ListItem("第"+i.ToString()+"轮",i.ToString());
45 al.Add(lst);
46 }
47 drp2.DataSource=al;
drp2.DataTextField="Text";
drp2.DataValueField="Value";
48 drp2.DataBind();
drp2.SelectedValue=num.ToString();
49 }
50 }
51}
52
2<%@ Control Language="c#" AutoEventWireup="false" Codebehind="UserControl1.ascx.cs" Inherits="Test.UserControl1.ascx.cs" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
3<asp:DropDownList id="drp1" AutoPostBack="True" runat="server"></asp:DropDownList>
4<asp:DropDownList id="drp2" AutoPostBack="True" runat="server"></asp:DropDownList>
5
6//UserControl1.ascx.cs
7using System;
8using System.Collections;
9using System.Data;
10using System.Web.UI;
11using System.Web.UI.WebControls;
12
13using namespace Test
14public class UserContorl1:UserControl
15{
16protected DropDownList drp1;
17protected DropDownList drp2;
18
19public void OnInit(int num1,int num2)
20{
21 BindDrp1(num1);
22 BindDrp2(num2);
23}
24//绑定drp1
25private void BindDrp1(int num)
26{
27 ArrayList al=new ArrayList();
28 ListItem lst;
29 for(int i=1;i<=num;i++)
30 {
31 lst=new ListItem("第"+i.ToString()+"轮",i.ToString());
32 al.Add(lst);
33 }
34 drp1.DataSource=al;
drp1.DataTextField="Text";
drp1.DataValueField="Value";
35 drp1.DataBind();
drp1.SelectedValue=num.ToString();
36}
37//绑定drp2
38private void BindDrp2(int num)
39{
40 ArrayList al=new ArrayList();
41 ListItem lst;
42 for(int i=1;i<=num;i++)
43 {
44 lst=new ListItem("第"+i.ToString()+"轮",i.ToString());
45 al.Add(lst);
46 }
47 drp2.DataSource=al;
drp2.DataTextField="Text";
drp2.DataValueField="Value";
48 drp2.DataBind();
drp2.SelectedValue=num.ToString();
49 }
50 }
51}
52
前面很简单,现在看我们在页面中怎样子调用前面的这个自定义控件了。
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebForm2" %>
<%@ Register TagPrefix="uc1" TagName="UserControl11" Src="UserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:UserControl1 id="UserControl11" runat="server"></uc1:UserControl1>
</form>
</body>
</HTML>
//部分后台代码
public class WebForm2 : Page
protected UserControl1 UserControl11;
//这句是重点,默认我们在添加的时候是不会添加的,
//要自己手动来添加,而且变量名要跟前面页面中一样
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
UserControl11.OnInit(10,20);
}
//这样就可以给他传值了
//下面还可以获得自定义控件上的控件
DropDownList drp1=UserControl11.FindControl("drp1") as DropDownList;
<%@ Register TagPrefix="uc1" TagName="UserControl11" Src="UserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:UserControl1 id="UserControl11" runat="server"></uc1:UserControl1>
</form>
</body>
</HTML>
//部分后台代码
public class WebForm2 : Page
protected UserControl1 UserControl11;
//这句是重点,默认我们在添加的时候是不会添加的,
//要自己手动来添加,而且变量名要跟前面页面中一样
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
UserControl11.OnInit(10,20);
}
//这样就可以给他传值了
//下面还可以获得自定义控件上的控件
DropDownList drp1=UserControl11.FindControl("drp1") as DropDownList;