How to dynamically add rows to a table in ASP.NET?

前端 未结 10 967
谎友^
谎友^ 2020-12-23 22:20

So today I started learning ASP.NET. Unfortunately I haven\'t found any good tutorials online, and I can\'t afford to buy books at the moment, so I\'ve had to create a ASP.N

相关标签:
10条回答
  • 2020-12-23 22:57

    in addition to what Kirk said I want to tell you that just "playing around" won't help you to learn asp.net, and there is a lot of free and very good tutorials .
    take a look on the asp.net official site tutorials and on 4GuysFromRolla site

    0 讨论(0)
  • 2020-12-23 22:57

    ASP.NET WebForms doesn't work this way. What you have above is just normal HTML, so ASP.NET isn't going to give you any facility to add/remove items. What you'll want to do is use a Repeater control, or possibly a GridView. These controls will be available in the code-behind. For example, the Repeater would expose an "Items" property upon which you can add new items (rows). In the code-front (the .aspx file) you'd provide an ItemTemplate that stubs out what the body rows would look like. There are plenty of tutorials on the web for repeaters, so I suggest you google that to obtain further information.

    0 讨论(0)
  • 2020-12-23 23:01

    Link for adding through JS https://www.youtube.com/watch?v=idyyQ23joy0

    Please see the below link as well. This would help you add the rows dynamically on the fly: https://www.lynda.com/C-tutorials/Adding-data-HTML-tables-runtime/161815/366843-4.html

    0 讨论(0)
  • Have you attempted the Asp:Table?

    <asp:Table ID="myTable" runat="server" Width="100%"> 
        <asp:TableRow>
            <asp:TableCell>Name</asp:TableCell>
            <asp:TableCell>Task</asp:TableCell>
            <asp:TableCell>Hours</asp:TableCell>
        </asp:TableRow>
    </asp:Table>  
    

    You can then add rows as you need to in the script by creating them and adding them to myTable.Rows

    TableRow row = new TableRow();
    TableCell cell1 = new TableCell();
    cell1.Text = "blah blah blah";
    row.Cells.Add(cell1);
    myTable.Rows.Add(row);
    

    Given your question description though, I'd say you'd be better off using a GridView or Repeater as mentioned by @Kirk Woll.

    EDIT - Also, if you want to learn without buying books here are a few sites you absolutely need to become familiar with:

    Scott Guthrie's Blog
    4 Guys from Rolla
    MSDN
    Code Project Asp.Net

    0 讨论(0)
提交回复
热议问题