where is this code? in Render? it is too late, dofor(int n=1;n<=nTotalPage;n++)
{
GoPage[n]=new LinkButton();
GoPage[n].Text=n.ToString();
GoPage[n].ID="GoPage"+n.ToString();
GoPage[n].Click+=new EventHandler(this.Gopageclicked);
    Controls.Add(GoPage[n]); }in Page_PreRender or before

解决方案 »

  1.   

    我是把它写在render里了,因为在它里面写了一个表格的几行,我必须把这几个按钮放在最后一行啊:
    protected override void Render(HtmlTextWriter htw)
    {
    htw.Write("<tr>");
    htw.Write("<td>");
    ...写其他内容
    htw.Write("</td>");
    htw.Write("</tr>");htw.Write("<tr>");
    htw.Write("<td>");
    ...写linkbutton
    htw.Write("</td>");
    htw.Write("</tr>");
    }
      

  2.   

    use Table/TableRow/TableCell to set up the layout in PreRender or before, do not hand write "<table>"/"<tr>"/"<td>"
      

  3.   

    see my replies to your previous posts
      

  4.   

    protected override void CreateChildControls() 
    {       
        



    int nTotalCount=Dtbname.Rows.Count;
    nTotalPage = nTotalCount/PageSize; 
    nTotalPage += nTotalCount % PageSize > 0? 1:0;
    LinkButton[] GoPage=new LinkButton[nTotalPage+2];
    for(int n=1;n<=nTotalPage;n++)
    {


    GoPage[n]=new LinkButton();
    GoPage[n].Text=n.ToString();
    GoPage[n].ID="GoPage"+n.ToString();
    GoPage[n].Click+=new EventHandler(this.Gopageclicked);
    }

    }

    private void Gopageclicked(Object sender, EventArgs e)
    {
    LinkButton lb = (LinkButton)sender;

              nStartcount=PageSize*(System.Convert.ToInt16(lb.ID.Substring(6))-1);

    }
      
    /// <param name="output"> 要写出到的 HTML 编写器 </param>

    protected override void OnPreRender(System.EventArgs e)
    {
    int nEndcount;
    if(nStartcount+PageSize>=dtbname.Rows.Count)
    {
    nEndcount=dtbname.Rows.Count;}
    else
    {
    nEndcount=nStartcount+PageSize;
    }
    for (int i=nStartcount; i < nEndcount; i++)
    {

    trcontent =new TableRow();
    //问题1:
                                       trcontent.BackColor=Trbgcolor;
    //问题2,第一列加连接:
    TableCell tc = new TableCell();
                    tc.Attributes.AddAttributes(href,"<a href="+Linkpage+"?"+Trans_guid+"="+Dt_guid+">");
    tc.Text=dtbname.Rows[i][Dt_guid].ToString();
    trcontent.Cells.Add(tc); for (int j=1; j < Columnum; j++)
    {
    tc.Text=dtbname.Rows[i][j].ToString();
    trcontent.Cells.Add(tc);
     
    }


    }
         
    //问题3,单独加一列,放linkbutton }我按你的写,有几个问题:
    1、列的背景色无法设,因为属性Trcolor是string类型
    2、无法给第一列的单元格加链接
    3、不会在最后一行加linkbutton
      

  5.   

    protected override void CreateChildControls() 
    {
     //...
     for (int i=nStartcount; i < nEndcount; i++)
     {

    LinkButton lb = new LinkButton();
    lb.Text= (nEndcount-nStartcount).ToString();
    lb.ID="GoPage" + n.ToString();
    lb.Click+= new EventHandler(this.Gopageclicked);
    trcontent =new TableRow();        
                            trcontent.BackColor=System.Drawing.ColorTranslator.FromHtml(Trbgcolor);TableCell tc = new TableCell();
                   
    tc.Controls.Add(lb);trcontent.Cells.Add(tc);//....
      }
    }
      

  6.   

    there are two ways to trigger an event handler related to a link
    1. shown above2. if you want to manually output <a href= ..your control needs to implement IPostBackEventHandler interface void RaisePostBackEvent(string eventArgument)
    {
     //do something based on eventArgument
    }protected override void Render(HtmlTextWriter writer)
    {
      for (int i=0; i < nRow; i++)
      {
        writer.Write("<tr><td>");
        writer.Write("<a href='javascript:" + GetPostBackEventReference(this, i.ToString()) + "'>" + "Sometext" + "</a>");
        writer.Write("</td>");
       ....
    }
      

  7.   

    问题就出在这里,linkbutton不能在render中创建,而给单元格里的内容加连接只能在render中实现(不通过linkbutton),但怎样创建一个既有连接又有linkbutton的tablero呢?
      

  8.   

    what do you mean by 一个既有连接又有linkbutton??? linkbutton is a 连接, use my second method if you insist
      

  9.   

    我说的连接特指超连接,类似htw.Write("<a href="+Url+">");可以调用另一页面
      

  10.   

    我不明白你的问题,如果使用htw.Write的话,你可以输出任何东西,有什么不可以的呢?如果想用LinkButton触发事件的话,用我上面说的第二种方法public YourControl : IPostBackEventHandler  ...
    {
    void RaisePostBackEvent(string eventArgument)
    {
      SomeMethod(eventArgument);}void SomeMethod(string eventArgument)
    {
       //do something based on eventArgument
    }protected override void Render(HtmlTextWriter writer)
    {
      for (int i=0; i < nRow; i++)
      {
        writer.Write("<tr><td>");
        writer.Write("<a href='javascript:" + GetPostBackEventReference(this, i.ToString()) + "'>" + "Sometext" + "</a>");
        writer.Write("</td>");
       //....
       }
      }
    }
      

  11.   

    在RENDER中可以用htw.Write输出LINKBUTTON么?你好象说过不可以啊!
      

  12.   

    我说的是,在Render里你不能做LinkButton lb = new LinkButton();
    SomeControl.Controls.Add(lb);这样生成的LinkButton也不能连接到事件方法上去但你总归可以做
     writer.Write("<a href='javascript:" + GetPostBackEventReference(this, i.ToString()) + "'>" + "Sometext" + "</a>");
      

  13.   

    不好意思,思归大侠,我刚刚学习作控件,我已知道了可以在CreateChildControls() 中做linkbuton并调用其方法,在render中写带有超连接的表格行,但如果想做这样的功能:先写n行带有超连接的表格行,然后在它后面的一行(n+1)加入linkbutton怎么实现?因为前面的是在render中实现,于是无法实现linkbutton了么?
      

  14.   

    public YourControl : Control, IPostBackEventHandler  //...另外的Interface
    {
    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
      SomeMethod(eventArgument);
    }void SomeMethod(string eventArgument)
    {
       //do something
      Page.Response.Write("I am clicked!");
    }protected override void Render(HtmlTextWriter writer)
    {
      //在这里先写n行带有超连接的表格行    writer.Write("<tr><td>");
        writer.Write("<a href='javascript:" + GetPostBackEventReference(this) + "'>" + "Sometext" + "</a>");
        writer.Write("</td></tr>");
       // .... 
      }
    }
      

  15.   

    here is a complete example (compile it to CustomControls.dll, add it the toolbox and drag/drop it to a page):using System;
    using System.Web.UI;namespace CustomControls 
    {
    public class MyLinkList : Control, IPostBackEventHandler
    {
    void IPostBackEventHandler.RaisePostBackEvent(string strEvent)
    {
    SomeMethod(strEvent);
    } void SomeMethod(string strEvent)
    {
    Page.Response.Write("it is " + DateTime.Now.ToString() + ". I am clicked!!!!");
    } protected override void Render(HtmlTextWriter htw)
    {
    htw.RenderBeginTag(HtmlTextWriterTag.Table);
    for (int i=0; i < 10; i++)
    {
    htw.RenderBeginTag(HtmlTextWriterTag.Tr);
    htw.RenderBeginTag(HtmlTextWriterTag.Td);
    htw.AddAttribute(HtmlTextWriterAttribute.Href,"http://www.csdn.net");
    htw.RenderBeginTag(HtmlTextWriterTag.A);
    htw.Write("CSDN");
    htw.RenderEndTag();

    htw.RenderEndTag();
    htw.RenderEndTag();
    } htw.RenderBeginTag(HtmlTextWriterTag.Tr);
    htw.RenderBeginTag(HtmlTextWriterTag.Td);
    htw.AddAttribute(HtmlTextWriterAttribute.Href,"javascript:" + Page.GetPostBackEventReference(this));
    htw.RenderBeginTag(HtmlTextWriterTag.A);
    htw.Write("Click Me!");
    htw.RenderEndTag();
    htw.RenderEndTag();
    htw.RenderEndTag(); htw.RenderEndTag();
    }

    }}
    TestPage.aspx:
    <%@ Register TagPrefix="cc2" Namespace="CustomControls" Assembly="CustomControls" %>
    <HTML>
    <body>
    <form id="WebForm1" method="post" runat="server">
    <cc2:MyLinkList id="MyLinkList1" runat="server"></cc2:MyLinkList>
    </form>
    </body>
    </HTML>
      

  16.   

    以下的linkbutton没放在第n+1行啊?
    for(int n=1;n<=nTotalPage;n++)
    {
    GoPage[n]=new LinkButton();
    GoPage[n].Text=n.ToString();
    GoPage[n].ID="GoPage"+n.ToString();
    GoPage[n].Click+=new EventHandler(this.Gopageclicked);
        Controls.Add(GoPage[n]); }
      

  17.   

    言尽于此,1. 如果你要用LinkButton,请在CreateChildControls里做,同时用Table/TableRow/TableCell/HyperLink2. 如果你要在Render里做,请看上面的例子