Search code examples
c#visual-web-developer

Programmatically building tables in Visual Web Developer C#


In my website I have an empty ASP table on a page like this:

<asp:Table ID="tblOverview" runat="server" BorderStyle="Double" 
            GridLines="Both" Width="927px" ForeColor="#333333" 
        CssClass="tblOverview">
</asp:Table>

I then run some code on page load, which essentially interacts with the table by adding rows and cells to it etc.

However now I want to use this table embedded within a gridview on another page, so I need to refactor this to be a bit more portable.

I'm not sure how to attack this though, should I change the method to return a table object? If I did that, how would I include it on the page like I do above? I'm having a slow morning, if someone can help me snap out of it I'll be very grateful! Thanks.


Solution

  • Would be great if you could change your method to non-static. I don't think it is good. But.. If the method which creates the links is static, make your event static too, and then you can assign to your 'Click' event of your button, for example.

    Something like this:

        public static void mTable()
        {
            Button btn = new Button();
            btn.Click += new EventHandler(btnClick);
        }
    
        protected static void btnClick(object sender, EventArgs e)
        { 
    
        }
    

    Ref to help.: Maybe you can look at MSDN Library article for the SystemEvents class. It tells something about static events.