天天看點

C#-web使用者控件

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;

}

}

轉載于:https://www.cnblogs.com/xianshui/p/4581051.html