DATALIST 如何给headertemplate内的label1.text赋值?

j9898 2004-11-21 05:42:05
DATALIST 如何用C#代码给headertemplate内的label1.text赋值?

例子中都是:

<HeaderTemplate>
<asp:Label id="Label1" runat="server" Width="264px" Height="24px">科技类图书</asp:Label>
</HeaderTemplate>

但我想用C#代码动态修改label1.text怎么办?
...全文
325 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
j9898 2004-11-21
  • 打赏
  • 举报
回复
解决了.多谢!

bitsbird(一瓢): 刚学,不会,以后多指点!!

nchen123 2004-11-21
  • 打赏
  • 举报
回复
如果用 OO 的写法,你可以用数据绑定语法 <%# %>
可以把要绑定的内容暴露为 property 或者 public method.

例如:

WebForm6.aspx
--------------------

<%@ Page language="c#" Codebehind="WebForm6.aspx.cs" AutoEventWireup="false" Inherits="MyPlayground.WebForm6" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm6</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataList id="dl" runat="server" DataSource='<%# GetDataSource() %>'>
<HeaderTemplate>
<asp:Label ID="lbl1" Runat="server"><%# DataListLabel %></asp:Label>
</HeaderTemplate>
</asp:DataList>
</form>
</body>
</HTML>

----------- 后台代码 WebForm6.aspx.cs ---------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyPlayground
{
/// <summary>
/// WebForm6 的摘要说明。
/// </summary>
public class WebForm6 : PageBase
{
protected System.Web.UI.WebControls.DataList dl;

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
this.DataBind();
}

public string DataListLabel
{
get
{
return "DataList Header";
}
}

public DataTable GetDataSource()
{
return SqlScope.ExecuteDataTable("select 1, 2, 3");
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
bitsbird 2004-11-21
  • 打赏
  • 举报
回复
forech(DataListItem Item in DataList1.Controls[0].Controls)
{
if(Item.Type==ListItemType.Header)
{
Label Label1=(Label1)Item.FindControl("Label1");
}
}
J老师也搞web开发了?
HardThink 2004-11-21
  • 打赏
  • 举报
回复
//In page
<asp:Label id="" Text= '<%labText%>' runat="server"/>

//in Code
protected string labText;
labText= "...";
loneghost 2004-11-21
  • 打赏
  • 举报
回复
DataList绑定的时候(ItemDataBound )
循环DataListItem,用((Label)FindControl("Label1")).Text = 你需要的值





 
 

 
.aspx页面
HeaderTemplate、ItemTemplate、FooterTemplate都有Label控件

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧