读书人

小弟我应该怎么使用findcotrol来找到L

发布时间: 2012-03-08 13:30:13 作者: rapoo

我应该如何使用findcotrol来找到Label1控件,
我用Datalist控件,来呈现新闻列表,想在后面显示时间(5/17),在后定义了一个Label控件,运行后提示当前下文中没有Label1

前台代码:

<asp:DataList ID= "dlSZ " runat= "server " >

<ItemTemplate>

<table class= "txt " >
<tr> <td style= "width:1px; "> </td> <td style= "width:300px ">
    <asp:LinkButton ID= "LinkButton1 " ForeColor= "#006666 " runat= "server " CommandName= "select " > <%# DataBinder.Eval(Container.DataItem, "title ") %> </asp:LinkButton>
</td>
<td style= "width:6px ">


<asp:Label ID= "Label1 " runat= "server " Text= "Label1 "> </asp:Label>


</td> </tr>
</table>
</ItemTemplate>
<HeaderStyle HorizontalAlign= "Center " />
<AlternatingItemStyle Font-Bold= "False " />
</asp:DataList>

后台代码:

public partial class _Default : System.Web.UI.Page
{


BaseClass bc = new BaseClass();
protected void Page_Load(object sender, EventArgs e)
{

dlSZ.DataSource = bc.GetDataSet( "SELECT TOP 10 * FROM xinwen WHERE (Type = '国内新闻 ') ", "xinwen ");
dlSZ.DataKeyField = "id ";
dlSZ.DataBind();

DataList1.DataSource = bc.GetDataSet( "SELECT TOP 5 * FROM tbtu order by addDate desc ", "tbtu ");
DataList1.DataKeyField = "id ";
DataList1.DataBind();

DataList2.DataSource = bc.GetDataSet( " SELECT TOP 5 * FROM article order by IssueDate desc ", "article ");
DataList2.DataKeyField = "id ";


DataList2.DataBind();
Label1.Text = System.DateTime.Now.ToString(MM/dd);
}

}
我应该如何使用findcotrol来找到Label1控件,应该怎样写代码.

谢谢

[解决办法]

foreach (Control c in PH1.Controls) //你控件在form中,换成form不知道成不成
{
if(c is label)
{
//
}
else
{
}
}

读书人网 >asp.net

热点推荐