zoukankan      html  css  js  c++  java
  • 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;
    }
    }

  • 相关阅读:
    数据绑定表达式语法(Eval,Bind区别)
    使用博客园的第一件事 自定义主题
    sql2000 跨服务器复制表数据
    使用UpdatePanel 局部刷新出现中文乱码的解决方法!!
    MMC不能打开文件MSC文件
    sql 日期 、时间相关
    loaded AS2 swf call function in AS3 holder
    Rewrite the master page form action attribute in asp.net 2.0
    100万个不重复的8位的随机数
    flash 中实现斜切变型
  • 原文地址:https://www.cnblogs.com/xianshui/p/4581051.html
Copyright © 2011-2022 走看看