C#-web用户控件

摘要:
公共委托代理的返回类型代理名称;publicdeletevoidShowDelegate;步骤2:使用上述代理类型定义代理变量。privateShowDelegateShow;步骤3:指定函数并将其分配给代理变量ShowvoidHaha{Console.WriteLine;}Show=newShowDelegate;步骤4:使用代理调用指向函数Show;页面上显示的两个用户控件,带有代理实例:用户控件1:usingSystem;使用System.Collections。通用的使用系统。Linq;使用系统。网状物使用System.Web。UI;使用System.Web.UI。Web控件;publicpartialclassSeach1:System.Web.UI。UserControl{privateCarsDataContextcontext=newCarsDataContext();publicdelegatevoidShowshuju;publicShowshujuShowdele;protectedvoidPage_Load{}protectedvoicecha_单击{varqu=context.Car.Where;if(Showdele!

C#-web用户控件第1张

从用户控件向页面中传递数据:
法一:使用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 runat="server" Text="请输入:"></asp:Label>
<asp:TextBox runat="server"></asp:TextBox>
<asp:Button 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 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="http://t.zoukankan.com/Seach1.ascx" tagname="Seach1" tagprefix="uc1" %>
<%@ Register src="http://t.zoukankan.com/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 runat="server">
<div>

<uc1:Seach1 runat="server" />
<br />
<br />
<br />
<uc2:Seach2 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 runat="server"></asp:TextBox>
<asp:Button runat="server" OnClick="Button1_Click" Text="控件(内)" />
<br />
<asp:Label 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="http://t.zoukankan.com/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 runat="server">
<asp:TextBox runat="server"></asp:TextBox>
<asp:Button runat="server" Text="外" />
<br />
<asp:Label runat="server" Text="Label外"></asp:Label>
<div>

<br />
<uc1:kongjian 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 runat="server" OnClick="Button1_Click" Text="代理" />
<asp:TextBox 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="http://t.zoukankan.com/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 runat="server">
<div>

<asp:TextBox runat="server"></asp:TextBox>
<asp:Button runat="server" OnClick="Button1_Click" Text="被代理" />
<br />
<br />
<br />
<br />
<asp:Label runat="server" Text="Label"></asp:Label>
<br />
<uc1:kongjian2 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;
}
}

免责声明:文章转载自《C#-web用户控件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C++ 之 伪随机数生成 <random>补习系列(19)-springboot JPA + PostGreSQL下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

plsql 建表空间

本文介绍使用plsql创建表空间和用户的方法。 方法/步骤              密码是ruc                             2、登陆成功后在界面的头部可以看到一下信息,最上面的system@ORCL就表示是用户system在登录状态,其中菜单栏的session可以登录、退出 3、然后在下面的列表中找到USER,然后右...

laravel中session的过期时间

在项目开发的过程中,前后端分离 需要用session保存用户的登陆信息 这就涉及到session的有效期了 session又分为php中的session有效期和laravel中的session的有效期 他们默认的有效期是 在php.ini中查看 session.gc_maxlifetime 默认是1440秒 差不多是24分 而laravel的sessio...

Shiro学习(10)Session管理

Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理、会话事件监听、会话存储/持久化、容器无关的集群、失效/过期支持、对Web的透明支持、SSO单点登录的支持等特性。即直接使用Shiro的会话管理可以直接替换如Web容器的会话管理。 会话 所谓会话,即用户访...

spring boot整合shiro

安全框架Shiro和Spring Security比较,本文主要围绕Shiro进行学习 一 Shiro 是一个强大而灵活的开源安全框架,能够清晰的处理认证 授权 管理会话以及,密码加密   01 .认证与授权相关概念     安全实体:  系统需要保护的具体对象数据     权限: 系统相关的功能操作,例如基本的CRUD     Authenticatio...

selenium webdriver 执行原理

selenium webdriver源码结构 Python版的源码结构,只看webdriver相关 selenium-masterpyseleniumwebdriver emote 下的文件 |-- command.py 命令相关...

爬虫实战篇(模拟登录)---我们以模拟去哪儿网为例

(1)、登录实质 互联网上的部分网站需要登录后方能访问,当我们打开网页并登录,就会在客户端生成Cookies(相当于个人身份证)信息,Cookies中包含了SessionId信息,登录后的请求都会带上Cookies发送给服务器,服务器会根据Cookies判断出对应的SessionID,进而找到会话,从而判断用户是否师登录状态,从而是否给用户响应。 (2)、...