佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 用戶控件(ASCX)向網(wǎng)頁(ASPX)傳值使用反射實(shí)現(xiàn)

用戶控件(ASCX)向網(wǎng)頁(ASPX)傳值使用反射實(shí)現(xiàn)

熱門標(biāo)簽:電子圍欄 阿里云 團(tuán)購網(wǎng)站 Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 銀行業(yè)務(wù) Linux服務(wù)器 服務(wù)器配置
用戶控件向網(wǎng)頁傳遞值,方法非常之多,此博文嘗試使用反射來實(shí)現(xiàn)。在站點(diǎn)中,建一個網(wǎng)頁以及一個用戶控件。 網(wǎng)頁切換至設(shè)計(jì)模式,拉用戶控件至網(wǎng)頁上。

Default.aspx:
復(fù)制代碼 代碼如下:

%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
%@ Register Src="InsusUC.ascx" TagName="InsusUC" TagPrefix="uc1" %>
!DOCTYPE html>
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
div>
uc1:InsusUC ID="InsusUC1" runat="server" />
br />
br />
Hi, You input infor as below:br />
first textbox value:
asp:Label ID="LabelshowFirstValue" runat="server" Text="" ForeColor="Red">/asp:Label>br />
Second textbox value:
asp:Label ID="LabelshowLastValue" runat="server" Text="" ForeColor="Red" >/asp:Label>
/div>
/form>
/body>
/html>

Default.aspx.cs,建一個帶兩個參數(shù)的public方法。
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void ReadUCMessage(string value1, string value2)
{
this.LabelshowFirstValue.Text = value1;
this.LabelshowLastValue.Text = value2;
}
}

接下來,我們創(chuàng)建一個用戶控件:
復(fù)制代碼 代碼如下:

%@ Control Language="C#" AutoEventWireup="true" CodeFile="InsusUC.ascx.cs" Inherits="InsusUC" %>
First Name asp:TextBox ID="TextboxFirstName" runat="server">/asp:TextBox>br />
Last Name asp:TextBox ID="TextboxLastName" runat="server">/asp:TextBox>br />
asp:Button ID="ButtonTransmit" runat="server" Text="Transmit" OnClick="ButtonTransmit_Click" />

寫銨鈕事件,首先引用namespace using System.Reflection;
有關(guān)type.InvokeMember()方法,可以參考msdn:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(v=vs.80).aspx
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
public partial class InsusUC : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonTransmit_Click(object sender, EventArgs e)
{
string v1 = this.TextboxFirstName.Text.Trim();
string v2 = this.TextboxLastName.Text.Trim();
this.Page.GetType().InvokeMember("ReadUCMessage", BindingFlags.InvokeMethod, null, this.Page, new object[] { v1,v2 });
}
}

演示:
您可能感興趣的文章:
  • asp.net 動態(tài)添加多個用戶控件
  • asp.net 用戶控件讀取以及賦值
  • asp.net 用戶控件中圖片及樣式問題
  • ASP.NET用戶控件技術(shù)

標(biāo)簽:衡水 蚌埠 廣元 大理 江蘇 萍鄉(xiāng) 衢州 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《用戶控件(ASCX)向網(wǎng)頁(ASPX)傳值使用反射實(shí)現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    平潭县| 安多县| 雷州市| 监利县| 东宁县| 额尔古纳市| 运城市| 清水河县| 宁津县| 扎囊县| 景德镇市| 汕尾市| 黄冈市| 克拉玛依市| 楚雄市| 芜湖县| 如皋市| 景宁| 威宁| 新源县| 汶上县| 盘锦市| 怀集县| 正安县| 老河口市| 伊通| 济宁市| 泗水县| 汽车| 阿合奇县| 和龙市| 清水县| 新宁县| 罗山县| 荥阳市| 金川县| 应城市| 洛南县| 宜君县| 新田县| 区。|