C#-web用户控件

从用户控件向页面中传递数据:
法一:使用Session传递。
1.在按钮点击时候,把值放到Session中去。
2.重写页面的OnLoadComplete方法,在这个方法中把值从Session中取出来。
注意:不要在Page_Load中取出Session 来。原因是:每次点击按钮的时候,Page_Load总是在按钮的Click之前触发。

法二:使用代理(委托 delegate)向页面传值
什么是代理?——代理是指向方法的指针。
代理与类非常相似但又很不相同。
类和对象:
第一步:使用class关键词定义一个新类
public class Ren
{
public void Speak()
{
....
}
}
第二步:使用这个类来定义变量。
private Ren r;

第三步:实例化Ren对象,把这个对象赋给栈里的变量
r = new Ren();

第四步:通过调用栈里的变量来实现对堆里的对象的操作。
r.xxxx

代理委托:
第一步:使用delegate关键词定义一个新的代理类型。
public delegate 代理的返回类型 代理名(参数列表);
public delete void ShowDelegate(string s);

第二步:使用上面的代理类型来定义代理变量。
private ShowDelegate Show;

第三步:指定一个函数,把这个函数赋给代理变量Show
void Haha(string aaa)
{
Console.WriteLine(aaa);
}
Show = new ShowDelegate(Haha);

第四步:使用代理调用指向的函数
Show("Hello");

两个用户控件,在页面上显示,代理实例:

用户控件1:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Seach1.ascx.cs" Inherits="Seach1" %>
<asp:Label ID="Label1" runat="server" Text="请输入:"></asp:Label>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="cha" runat="server" OnClick="cha_Click" Text="查询" />

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

public partial class Seach1 : System.Web.UI.UserControl
{
private CarsDataContext context = new CarsDataContext();
public delegate void Showshuju(List<Car> list);
public Showshuju Showdele;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void cha_Click(object sender, EventArgs e)
{
var qu = context.Car.Where(p => p.Name.Contains(txt.Text));
if (Showdele != null)
{
Showdele(qu.ToList());
}
}
}

用户控件2:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Seach2.ascx.cs" Inherits="Seach2" %>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

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

public partial class Seach2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
public void BindCar(List<Car> list)
{
GridView1.DataSource = list;
GridView1.DataBind();
}
}

显示页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Seach-ye.aspx.cs" Inherits="Seach_ye" %>

<%@ Register src="Seach1.ascx" tagname="Seach1" tagprefix="uc1" %>
<%@ Register src="Seach2.ascx" tagname="Seach2" tagprefix="uc2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<uc1:Seach1 ID="Seach11" runat="server" />
<br />
<br />
<br />
<uc2:Seach2 ID="Seach21" runat="server" />

</div>
</form>
</body>
</html>

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

public partial class Seach_ye : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Seach11.Showdele = new Seach1.Showshuju(Seach21.BindCar);
}
}

一个用户控件,在页面上显示,SESSION实例:

控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="kongjian.ascx.cs" Inherits="kongjian" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="控件(内)" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label内"></asp:Label>

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

public partial class kongjian : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Session["aa"] = TextBox1.Text;
}
}

显示页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="页面.aspx.cs" Inherits="页面" %>

<%@ Register src="kongjian.ascx" tagname="kongjian" tagprefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="外" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label外"></asp:Label>
<div>

<br />
<uc1:kongjian ID="kongjian1" runat="server" />

</div>
</form>
</body>
</html>

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

public partial class 页面 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
if (Session["aa"] != null)
{
TextBox1.Text = Session["aa"].ToString();
}
}
}

一个用户控件,在页面上显示代理实例:

控件:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="kongjian2.ascx.cs" Inherits="kongjian2" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="代理" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

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

public partial class kongjian2 : System.Web.UI.UserControl
{
public delegate void Showdele(string str);
public Showdele textdele;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (textdele != null)
{
textdele(TextBox1.Text);
}
}
}

显示页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="页面2.aspx.cs" Inherits="页面2" %>

<%@ Register src="kongjian2.ascx" tagname="kongjian2" tagprefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="被代理" />
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<uc1:kongjian2 ID="kongjian21" runat="server" />

</div>
</form>
</body>
</html>

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

public partial class 页面2 : System.Web.UI.Page
{
private delegate void Textdele(string str);
private Textdele Deletext;
protected void Page_Load(object sender, EventArgs e)
{
kongjian21.textdele = new kongjian2.Showdele(setshow);
}
protected void Button1_Click(object sender, EventArgs e)
{
Deletext = new Textdele(Show);
Deletext("这个代理所做!");
}
public void Show(string s)
{
Label1.Text = s;
}
public void setshow(string s)
{
Label1.Text = s;
}
}

时间: 2024-08-05 09:09:35

C#-web用户控件的相关文章

自定义web用户控件ascx

在页面中使每个产品类别都展示重复的样式又想代码简洁,这时就要设置一个自定义控件. 拖入一个Repeater控件设置好样式 在page_load事件下面写一个属性: protected voidPage_Load(object sender, EventArgs e) { if(!isPostBack) { var data=new T_UserTableAdapter().GetDataById(CatId); Repeater1.DataSource=data; //手动绑定控件 Repeat

ASP.NET web用户控件

我们在编写网站时,除了使用它们固定的服务器控件,我们还可以自定义一些控件来重复使用. 添加一个web用户控件,可以在前端和后台添加内容,再添加一个web窗体,将web用户控件拖入web窗体对应地方中. 注意,用户控件不能设为起始页,并且拖入web窗体时,要将web窗体改为设计模式,方可拖入成功. 拖入后的代码如下: 前端: <%@ Register src="ww.ascx" tagname="ww" tagprefix="uc1" %&g

js清空web用户控件的值

假设你的用户控件里面有: <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="111" Value="111"></asp:ListItem> </asp:DropDownList> 然后你在aspx页面中注册这个控件: <%@ Register Src="~/WebUs

Web用户控件

用户控件: 1.控件路径,Image,超链接 加一个runat="server"将该控件变成服务器控件,服务器会自动转换路径

Web用户控件开发--星型评分控件

本文中分享一个实现简单,使用方便的星型评分控件. 一:贴几张测试图片先: 二.星型评分控件的实现: RatingBar.ascx: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RatingBar.ascx.cs" Inherits="UserControls.Controls.RatingBar" %> <style type=&q

net7:Web用户控件ascx的使用及其动态加载

原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] Web用户控件test.ascx的源代码: using System;using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebCon

[转]Oracle分页之二:自定义web分页控件的封装

本文转自:http://www.cnblogs.com/scy251147/archive/2011/04/16/2018326.html 上节中,讲述的就是Oracle存储过程分页的使用方式,但是如果大量的页面要使用这个分页存储过程,如果利用上节的方式,势必要书写大量的代码.如何才能够少些代码书写量呢?当然了,利用自定义web控件进行一下封装,也许是一个好方法,但是如何进行封装呢? 首先,就是在项目中添加一个“Web 用户控件“的页面,我们定义为:MyPagination.ascx 然后,就是

使用用户控件(2)

3.2.2 使用用户控件(2) (3) 打开需要添加搜索框的页面,这里是Index.aspx. (4) 在页面代码头部的@Page指令下面添加一行代码,如下所示: <%@ Register src="WUCSearch.ascx" tagname="WUCSearch" tagprefix="uc1" %> 该代码将把刚才我们创建的用户控件WUCSearch.ascx注册到这个页面上.以后就可以直接在这个页面的任何地方使用了. (5)

用户控件&amp;自定义控件----.Net再学

开始学习.Net到现在两年了快,开始学习java 也有大半年了,中间做项目,又开始.Net的学习.这次做.Net项目,后台代码敲的不是太多,重点放在了UI上,这也是这篇博客要写的内容有关,做UI就少不了控件的使用.这次做UI 主要是提取公共,抽象控件. 敲代码,就离不开控件,那是第一次.Net学习过之后,对于控件的认识很肤浅,通过这次学习,又弥补了对UI控件的认识.这篇博客要说的是:用户控件和自定义控件.提到这两种控件,想必编程的孩子们都不陌生,但是谈到使用,谈到他们的区别,伙伴们清楚吗? 我们