2

I'd like some guidance on the approach I'm taking to creating an ASP.NET Web Application.

The application (in the .aspx.cs file) is designed to look in a directory at a set of text files. For each text file I would like to create/amend a table in my .aspx file using information from the text file.

So far I have created asp:Table controls with ID attributes. I then build up a table in my .aspx.cs file using TableRow & TableCell methods and add them to the table using the ID attribute.

This has all gone well and is all hard coded in. I have begun unpicking it and creating a for loop to loop through the directory looking for text files. It then grabs the textfile name and I would like to use that to amend an asp:table control with the same name. However I cannot call the same table methods on the textfile name since it is a string.

So finally my question is, is this the right approach? Do I hard code asp:table controls in my .aspx file of tables I will know the names of (the textfile names) and then try and update them using the C# behind code? If so is it just a case of creating an instance of a table in C# using the textfile name which will bind it to the asp:table control?

Sorry if this is very convoluted - first time using asp.net today.

3
  • 1
    Not that i get the question 100%, but why don't you just go with a single panel, and then create the table, rows and cells whe nyou need them and when you can set them, and then add them to the panel. IT also might be better to use repeater, depending on your data, can't tell much by the question though. Commented Nov 11, 2015 at 16:44
  • This is what I've done in the end. I was confusing myself by trying to do half the work in the .aspx file and half in the .aspx.cs file. I added a master panel to the .aspx file and then continued to do all of the work in the .aspx.cs file. Thanks. Commented Nov 13, 2015 at 8:49
  • Yeah, that should work just fine as it is. If you really wana do half and half, you can get your data and feed it to a repeater and use their OnItemDataBound to play with the data, etc. Commented Nov 13, 2015 at 11:35

1 Answer 1

1

You should be able to create the table controls from the code behind within the loop that processes the files. You can have a placeholder control in the markup and add the table controls to the control collection for the place holder.

In .aspx: <asp:PlaceHolder runat="server" ID="tablePlaceHolder"></asp:PlaceHolder>

In .aspx.cs:

  foreach (var file in files) {
    var id = GetIdFromFileName(file); // derive ID from file
    var tblControl = new Table {ID = id};
    tablePlaceHolder.Controls.Add(tblControl);

    // create rows and cells for the table control as appropriate 
    // based on the content of the file
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.