我用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控件,应该怎样写代码.
谢谢
<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控件,应该怎样写代码.
谢谢
解决方案 »
- asp.net 导入txt excel文件的速度问题
- 如何将GridView中的数据导出到以csv为后辍名的Excel中去?
- "HTTP/1.1 500 Internal Server Error"
- 关于两个日期相减,得到的结果看不懂,不知道具体是多少时间
- 有没有可能让DATAGRID中的同一绑定列中出现不同的绑定控件???????????????
- ASP.NET中GridView.RowUpdating 事件?(我看的是.NET2003的书籍,用的是.NET2005)
- 怎么在SQL SERVER数据库中读取图片文件并显示在GRIDVIEW里?
- 如何设定按钮为一个页面上的默认按钮,即打开该页面回车是激发该按钮的事件处理程序
- 怎样才可以对HtmlTable控件中已经存在的cell设置属性啊,是不是只能在动态中加入的cell中设置属性啊?
- 请问,在datagrid控件中怎样才能实现编辑呢?
- 关于web页面上网络图的问题
- 刚刚学习C#,原来是做VB的C/S开发的,如何能少走弯路,请C友们指点.
foreach (Control c in PH1.Controls) //你控件在form中,换成form不知道成不成
{
if(c is label)
{
//
}
else
{
}
}
if(c is label)
{
//
}
else
{
}
}
你可以这样理解,现在你能获取,page.listbox,也能获取page.listbox.label(逻辑概念,程序中不能这么写)
但你不能获取page.label,因为label不是直接在page中的。可以这样
foreach(ListItem dlt in dlSZ.Items)
{
Label lbTarget = (Label)dlt.cell[0].FindeControls["Label1"];
}
大意如此,需要你尝试调试。
foreach(DataListItem dgi in dlSZ.Items)
{
Label lb_Target = (Label)dgi.FindControl("Label1");
}
foreach(DataListItem dgi in dlSZ.Items)
{
Label lb_Target = (Label)dgi.FindControl("Label1");
}
Label1.Text = System.DateTime.Now.ToString(MM/dd); 我实在不知道怎么写??
这就是获取系统当前时间的方法。foreach(DataListItem dgi in dlSZ.Items)
{
Label lb_Target = (Label)dgi.FindControl("Label1");
}
这些是放在你希望捕捉控件的时间里面,比如button_click
<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><ItemTemplate>中的控件只有在DataBind()的时候才会创建,不信你可以单步调试下看看dlSZ.Items.Count只有在绑定数据的时候才会增加,其他时候dlSZ.Items.Count是0