小弟想通过一个循环语句自动生成web控件,并在页面上显示出来,但不知为何没有显示出来,请大家帮忙看看?
for ( int i = 1; i < 7; i++ )
{
    HyperLink hyl = new HyperLink();
    hyl.ID="hyl" + i.ToString();
    hyl.Text="abc" + i.ToString();
 }

解决方案 »

  1.   

    动态创建控件例子:
    <%@page language="C#"%>
    <%@import namespace="System.Data"%>
    <%@import namespace="System.Data.OleDb"%>
    <%@import namespace="System.Text"%><style type="text/css">
       TD.ProductDesc
       {
     font-family: Verdana;
            font-size: small;
     color: #FF3333;
     width: 100%;
     vertical-align: top;
       }
    </style>
    <script language="c#" runat="server"> Hashtable hashProductName = new Hashtable();
     Hashtable hashProductDesc = new Hashtable();public void page_load(Object obj,EventArgs e)
    { hashProductName[0] = "Jalapeno Dip" ;
     hashProductName[1] = "Smoked Sausage" ;
     hashProductName[2] = "Shrimp Fiesta" ;
     hashProductName[3] = "Jerk Chicken" ;
     hashProductName[4] = "Beer-Battered Fish" ;
     hashProductName[5] = "Bacon Burger" ;
     hashProductName[6] = "Sirloin Tip" ;
     hashProductName[7] = "Baked Alaska" ;
     hashProductName[8] = "Fried Chicken" ;
     hashProductName[9] = "Fresh Garden Salad" ;
     hashProductName[10] = "One Pea" ; hashProductDesc[0] = "Simmered in mayonaise and wine, this Jalapeno Dip will make your eyes water" ;
     hashProductDesc[1] = "Mouth watering and delicious sausage" ;
     hashProductDesc[2] = "East Coast's finest shrimp" ;
     hashProductDesc[3] = "A real island experience you will not forget" ;
     hashProductDesc[4] = "Pabst Blue Ribbon and Fish. Wow!" ;
     hashProductDesc[5] = "Big, juicy, and bursting with flavor" ;
     hashProductDesc[6] = "Delicate cuts with no fat" ;
     hashProductDesc[7] = "Fine dessert comprised of sponge cake topped with ice cream and covered with meringue. " ;
     hashProductDesc[8] = "Country cookin'" ;
     hashProductDesc[9] = "Crispy iceberg lettuce and a garden of vegtables" ;
     hashProductDesc[10] = "A single green pea that will leave you craving more" ; for (int i=0; i<=10; i++)
     {
       LinkButton LB5= new LinkButton();
       LB5.Text = hashProductName[i].ToString();
       LB5.CommandName = "Products";
       LB5.CssClass = "ProductLinks";
       LB5.CommandArgument = i.ToString() ;
       LB5.Command += new System.Web.UI.WebControls.CommandEventHandler(OnLinkClick);
       LinkList.Controls.Add(LB5);
       LinkList.Controls.Add(new LiteralControl("
    "));
     }
    }private void OnLinkClick(object O, System.Web.UI.WebControls.CommandEventArgs E)
    {
     int RecordId = Int32.Parse(E.CommandArgument.ToString());
     tablecellMessage.Text="<b>"+hashProductName[RecordId].ToString()+"</b>
    <i>"+hashProductDesc[RecordId].ToString()+"</i>";
    }
    </script><form runat="server"><asp:Table CellPadding=6 CellSpacing=2 BorderColor="#DDDDDD" BorderStyle=Solid BorderWidth=2 Runat=server>
    <asp:TableRow Runat=server>
      <asp:TableCell id=LinkList Wrap=False BackColor="#FFFFFF" Runat=server/>
      <asp:TableCell id="tablecellMessage" CssClass="ProductDesc" Runat=server></asp:TableCell>
    </asp:TableRow>
    </asp:Table></form>
    <asp:label id="message" forecolor="red" runat="server"/>
      

  2.   

    for ( int i = 1; i < 7; i++ )
    {
        HyperLink hyl = new HyperLink();
        hyl.ID="hyl" + i.ToString();
        hyl.Text="abc" + i.ToString();
        Page.Controls.Add(hy1);
    }
      

  3.   

    楼主没有绑定啊
    建议如果需要有次序排列,最好还是用
    前台
    <table id="xx" runat="server">
    后台
    HtmlTableRow hr=new HtmlTableRow();
    HtmlTableCell hc=new HtmlTableCell();
    for ( int i = 1; i < 7; i++ )
    {
        HyperLink hyl = new HyperLink();
        hyl.ID="hyl" + i.ToString();
        hyl.Text="abc" + i.ToString();
    }
    hc.Controls.Add(hy1);
    hr.Cells.Add(hc);
    xx.Rows.Add(hr);
    这样还可以控制样式等
      

  4.   

    主要是少了Page.Controls.add()语句.
    生所的控件没被加载到页面中.
      

  5.   

    动态生成放在一个Panel控件中
    foreach(FileInfo file in files)
    {
    k++;
    if(file.Extension.ToLower()!=".htm" && file.Extension.ToLower()!=".txt" && file.Extension.ToLower()!=".swf")
    continue;
    HyperLink hy=new HyperLink();
    hy.Font.Size=FontUnit.XSmall;
    hy.Target="_blank";
    hy.ID="xhy_"+k.ToString();
    hy.NavigateUrl="xingzhuo\\"+file.Name;
    this.Panel2.Controls.Add(hy); hy.Text=file.Name.Substring(0,file.Name.Length-4);
    }
      

  6.   

    现在用html的table可以显示,用web控件的table出错,而且2者都不能换行显示,怎么办呀,用了 xulovewei(耗子爱猫咪)的方法,但换行好像不行,没起作用呀
      

  7.   

    参考一下这个:http://community.csdn.net/Expert/topic/3370/3370620.xml?temp=.2362024
      

  8.   

    new 出来的控件不要忘了使用Controls.Add()添加到页面上,你可以先方一个控件容器PlaceHolder来添加你的这些控件!
    如下:
    for ( int i = 1; i < 7; i++ )
    {
        HyperLink hyl = new HyperLink();
        hyl.ID="hyl" + i.ToString();
        hyl.Text="abc" + i.ToString();
        Literal lbr = new Literal();
        lbr.Text = "<br>";
        this.PlaceHolder1.Controls.Add(hy1);
        this.PlaceHolder1.Controls.Add(lbr);
     }