zoukankan      html  css  js  c++  java
  • 使用反射把用户控件(ASCX)传至网页(ASPX)

    用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现。在站点中,建一个网页以及一个用户控件。 网页切换至设计模式,拉用户控件至网页上。

    Default.aspx:

    View Code
    <%@ 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,建一个带两个参数的public方法。

    View Code
    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;
        }
    }


    接下来,我们创建一个用户控件:

    View Code
    <%@ 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;
    有关type.InvokeMember()方法,可以参考msdn:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(v=vs.80).aspx

    View Code
    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 });
        }
    }


    演示:

  • 相关阅读:
    子程序的设计
    多重循环程序设计
    汇编语言的分支程序设计与循环程序设计
    代码调试之串口调试2
    毕昇杯模块之光照强度传感器
    毕昇杯之温湿度采集模块
    【CSS】盒子模型 之 IE 与W3C的盒子模型对比
    【css】盒子模型 之 概述
    【css】盒子模型 之 弹性盒模型
    【网络】dns_probe_finished_nxdomain 错误
  • 原文地址:https://www.cnblogs.com/insus/p/2951668.html
Copyright © 2011-2022 走看看