加个runat=server也可以找到啊!不用转成服务端控件;
还有如果是在用户自定义控件里找:
取得用户控件里面的控件并进行赋值
用户控件aspx页代码,下面是个参考:
复制代码- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="HeadPanel.ascx.cs" Inherits="HeadPanel" %>
- <asp:Label ID="lb1" runat="server" Text=""></asp:Label> //在用户控件里定义 的两个控件
- <asp:Label ID="lb2" runat="server" Text=""></asp:Label>
|
cs页代码
复制代码- 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 = "用户控件进行赋值的标题";
- public string tmpStr="这是用户变量";
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
|
引用用户控件aspx页代码
复制代码- <%@ 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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
- <title><%= HeadPanel.tmpSiteName%></title> //HeadPanel是用户控件cs页的类名,tmpSiteName是用户控件里的静态变量
- </head>
- <body>
- </body>
- </html>
|
引用用户控件cs页代码
复制代码- 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面进行的赋值111"; //对用户控件里的控件进行赋值
- HeadPanel hp=new HeadPanel(); //实例用户控件后台类
- Label lb2= (Label)ucHead.FindControl("lb2");
- lb2.Text = "这是default面进行的赋值"+ hp.tmpStr;
- // HeadPanel.tmpSiteName //这个静态变量可以直接调用
- }
| 还有如果你在datalist等控件里面找,必须得在他的itemdatabound事件里:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
foreach (DataListItem ditem in DataList1.Items)
{
string cid = ((Label)ditem.FindControl("lblcid")).Text;
string uid = ((Label)ditem.FindControl("lbluser")).Text;
string recontent=((TextBox)ditem.FindControl("recontent")).Text;
DataList dl2 = (DataList)ditem.FindControl("DataList2");
foreach (DataListItem dlitem in dl2.Items)
{
string userid = ((Label)dlitem.FindControl("lblreuserid")).Text;
if (userid != null)
{
User model = UserBLL.GetUserById(int.Parse(userid));
Session["replytor"] = model;
}
}
Button rebtn= (Button)ditem.FindControl("rebtn");
rebtn.Click += new EventHandler(rebtn_Click);
if (cid != null)
{
dl2.DataSource = ReplyBLL.select_by_cid(int.Parse(cid));
dl2.DataBind();
}
if (uid != null)
{
User model = UserBLL.GetUserById(int.Parse(uid));
Session["commentor"] = model;
}
}
}
|