在.ascx中:
<asp:datalist id="MenuDataList" RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<asp:LinkButton Runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"HeaderName")%>' OnCommand="LinkButton_Command">
<%#DataBinder.Eval(Container.DataItem,"HeaderName")%>
</asp:LinkButton>
<br>
<br>
</ItemTemplate>
</asp:datalist>
在.ascx.cs中:
    using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
    using System.Data.SqlClient ;
/// <summary>
/// HeaderMenu 的摘要说明。
/// </summary>
public abstract class HeaderMenu : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataList MenuDataList; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string  StringDatabase="server=LJJ;uid=sa;pwd=;database=crmmenu";
SqlConnection Myconn=new SqlConnection(StringDatabase);
SqlDataAdapter MyAdapter=new SqlDataAdapter("select * from  Header order by AutoID" ,Myconn);
DataSet  menu=new DataSet();
MyAdapter.Fill(menu,"menu");
MenuDataList.DataSource = menu;
MenuDataList.DataBind();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

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

}
private  string url;
public string LinkButtonText
{
get
{
EnsureChildControls();
return   url ;
}
}
//以下为定义一个事件,事件与menu.aspx响应
public event EventHandler Left;
      
protected virtual void OnLeft(EventArgs e)
{
if (Left!= null)
{
Left(this,e);
}
}
protected void LinkButton_Command(Object sender, CommandEventArgs e) 
{
url=e.CommandArgument.ToString();
OnLeft(new EventArgs());
}
}

解决方案 »

  1.   

    dim tabruntime as 
    if (tabruntime is nothing) then return
    dim lbl1 as label
    lbl1 = new label()
    lbl1.style.add("position","absolute")
    lbl1.style.add("top","40px")
    lbl1.style.add("left","50px")
    lbl1.font.bold = true
    lbl1.font.italic = true
    lbl1.text = "asdfasf"
    tabruntime.contentpane.children.add(lbl1)
      

  2.   

    肯定是要用循环。 如果动态写事件不好处理的话,建议你采用自定义控件(WEB USER CONTROL)来封装ListButton(有ListButton吗?)及其对应的事件,提供public的属性或方法供外部调用。根据查询的结果动态装载webusercontrol控件。这样就简单多了。重用性得到提高。
      

  3.   

    十分关注!!!!
    label可以使用dick_lai(君)的方法,但是textbox和button好像不可以直接添加!
    关注关注关注关注
      

  4.   

    完全可以做到阿,同意spring_ok的说法
      

  5.   

    谢谢各位的高论,我现在刚刚学习,对CLASS的概念还不是很熟悉,恳请各位高手
    在给原代码的时候,可以加上注释,这样小弟也不用很费神的去到MSDN中去查找了,有时候我的需要和大家给的还是有相当的出入的,故而非常感谢大家在解答
    小弟问题的时候给点注释!!再一次的感谢各位!!!!!谢谢!!!!
      

  6.   

    不好意思啦!!!应该是LinkButton啦!!!小弟一时疏忽,键入的时候搞错了!TO :spring_ok(sping.z)
       由于小弟的水平有限,可不可以给小弟我举一点例子!!!谢谢!!!!!
      

  7.   

    TextBox[] mytextbox=new TextBox[10];
    for (int i=0,i<10,i++)
    { mytextbox[i]=new TextBox();
    mytextbox[i].id=i.toString();
    mytextbox[i].text="11";
    this.controls[1].controls.add(mytextbox[i]);
    }
    事件可以一起加
      

  8.   

    Table2.Controls.Clear()
            Dim MySsBu() As Button
            Dim Row As TableRow
            Dim Cell As TableCell
            If Label1.Text = "" Then
                Label1.Text = TPID
            End If        SqlCmd("select * from TPGT where TPID='" & Label1.Text & "' order by SJ desc")
            Dim a As Integer
            Do While a < SqldataS.Tables(0).Rows.Count
                Row = New TableRow()            Cell = New TableCell()
                Cell.BackColor = Color.PaleGreen
                Cell.HorizontalAlign = HorizontalAlign.Center
                Cell.Text = SqldataS.Tables(0).Rows(a).Item("GTR")
                Row.Controls.Add(Cell)            Cell = New TableCell()
                Cell.HorizontalAlign = HorizontalAlign.Center
                Cell.BackColor = Color.Moccasin
                Cell.Text = SqldataS.Tables(0).Rows(a).Item("SJ")
                Row.Controls.Add(Cell)            Table2.Controls.Add(Row)            Row = New TableRow()            Cell = New TableCell()
                Cell.Width = System.Web.UI.WebControls.Unit.Pixel(600)
                Cell.ColumnSpan = 2
                Cell.BackColor = Color.LightBlue
                Cell.HorizontalAlign = HorizontalAlign.Center
                Cell.Text = SqldataS.Tables(0).Rows(a).Item("NR")
                Row.Controls.Add(Cell)            Table2.Controls.Add(Row)            If UserJ = "S" Then
                    ReDim MySsBu(a)
                    MySsBu(a) = New Button()
                    MySsBu(a).BorderStyle = BorderStyle.None
                    MySsBu(a).BackColor = Color.Aqua
                    MySsBu(a).ID = SqldataS.Tables(0).Rows(a).Item("ID")
                    MySsBu(a).ToolTip = MySsBu(a).ID
                    MySsBu(a).Text = "删除"
                    AddHandler MySsBu(a).Click, AddressOf ShiShan
                    Row = New TableRow()                Cell = New TableCell()
                    Cell.ColumnSpan = 2
                    Cell.BackColor = Color.Thistle
                    Cell.HorizontalAlign = HorizontalAlign.Center
                    Cell.Controls.Add(MySsBu(a))
                    Row.Controls.Add(Cell)
                    Table2.Controls.Add(Row)
                Else
                    Row = New TableRow()                Cell = New TableCell()
                    Cell.ColumnSpan = 2
                    Cell.BackColor = Color.Thistle
                    Cell.HorizontalAlign = HorizontalAlign.Center
                    Cell.Text = ""
                    Row.Controls.Add(Cell)
                    Table2.Controls.Add(Row)            End If
                Row = New TableRow()            Cell = New TableCell()
                Cell.Height = System.Web.UI.WebControls.Unit.Pixel(10)
                Row.Controls.Add(Cell)
                Table2.Controls.Add(Row)
                a = a + 1
            LoopPrivate Sub ShiShan(ByVal sender As System.Object, ByVal e As System.EventArgs)
            SqlCmd("delete from TPGT where ID='" & sender.ID & "'")
            Response.Redirect("bbsgt.aspx")    End Sub