取得用戶控件里面的控件并進(jìn)行賦值
用戶控件aspx頁代碼
復(fù)制代碼
- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HeadPanel.ascx.cs" Inherits="HeadPanel" %>
- <asp:Label ID="lb1" runat="server" Text=""></asp:Label> //在用戶控件里定義 的兩個(gè)控件
- <asp:Label ID="lb2" runat="server" Text=""></asp:Label>
|
cs頁代碼
復(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 HeadPanel : System.Web.UI.UserControl
- {
- public static string tmpSiteName = "用戶控件進(jìn)行賦值的標(biāo)題";
- public string tmpStr="這是用戶變量";
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
-
|
引用用戶控件aspx頁代碼
復(fù)制代碼
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Src="HeadPanel.ascx" TagName="RegHead" TagPrefix="uc" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./1999/xhtml">
- <head id="Head1" runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
- <title><%= HeadPanel.tmpSiteName%></title> //HeadPanel是用戶控件cs頁的類名,,tmpSiteName是用戶控件里的靜態(tài)變量
- </head>
- <body>
- </body>
- </html>
|
引用用戶控件cs頁代碼
復(fù)制代碼
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Text;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- init();
- loading();
- }
- Label lb1= (Label)ucHead.FindControl("lb1");
- lb1.Text = "這是default面進(jìn)行的賦值111"; //對(duì)用戶控件里的控件進(jìn)行賦值
- HeadPanel hp=new HeadPanel(); //實(shí)例用戶控件后臺(tái)類
-
- Label lb2= (Label)ucHead.FindControl("lb2");
- lb2.Text = "這是default面進(jìn)行的賦值"+ hp.tmpStr;
- // HeadPanel.tmpSiteName //這個(gè)靜態(tài)變量可以直接調(diào)用
- }
|