0

How do I take my C# array and write it to an HTML table header?

From what I've read you should be able to use cnippets to get the variable but it's not working...

C# code

protected void Page_Load(object sender, EventArgs e)
        {
            string[] tableHeaders = {
                "Urgency", 
                "Picker", 
                "CARD", 
                "Case Order", 
                "CR #", 
                "Patient", 
                "Age", 
                "ASA", 
                "Anes Consult", 
                "Procedure", 
                "Booking Surgeon", 
                "Operating Surgeon", 
                "Ready Date", 
                "Ready Time", 
                "NPO Since", 
                "EST Time", 
                "Current Location", 
                "Postop Need", 
                "Booked by", 
                "Special Equipment", 
                "Comments"
            };
        }

HTML Code You'll see in my document.write line I try to call the tableHeaders variable. And I use the javascript row variable as a counter.

<thead>
  <tr>
    <script type="text/javascript">
      for (var row = 0; row <= 21; row++) {
        document.write("+<%= tableHeaders[+row+] %>+")
      }
    </script>
   </tr>
 </thead>
5
  • Make tableheaders a global property Commented Jul 23, 2014 at 14:51
  • Are you trying to create dynamic <th> elements in your row or just write out a list of header names? Commented Jul 23, 2014 at 14:53
  • Yes. I've just been told that you can't put C# variables in java-script so that kind of means my question isn't valid. Commented Jul 23, 2014 at 14:57
  • @Frantumn: I wouldn't say your question isn't valid as much as your attempted solution won't get the job done as you've envisioned it. Though an edit to the question might make it a little more clear. Commented Jul 23, 2014 at 14:59
  • Maybe you want to use RegisterArrayDeclaration. Commented Jul 23, 2014 at 15:01

3 Answers 3

1

I shouldn't have been trying to use Java-script to do this. The following worked perfectly.

 <%
     for (int td = 0; td <= 20; td++)
     {
         Response.Write("<td>" + tableHeaders[td] + "</td>");
     }
 %>
Sign up to request clarification or add additional context in comments.

Comments

0

You are creating a local variable in Page_Load then trying to access it from the page script. You can make it a class member instead:

(outside of Page_Load or any other method):

private string[] tableHeaders = {
    "Urgency", 
    "Picker", 
    "CARD", 
    "Case Order", 
    "CR #", 
    "Patient", 
    "Age", 
    "ASA", 
    "Anes Consult", 
    "Procedure", 
    "Booking Surgeon", 
    "Operating Surgeon", 
    "Ready Date", 
    "Ready Time", 
    "NPO Since", 
    "EST Time", 
    "Current Location", 
    "Postop Need", 
    "Booked by", 
    "Special Equipment", 
    "Comments"
};

Comments

0

Assuming you want to create actual header cells for your table, you should be able to do it all within c# code behind unless you have some need to dump things into JavaScript. Add an ID to your <tr> element and a runat attribute:

<thead>
    <tr ID="headerRow" runat="server">
    </tr>
</thead>

Then in your page load you can add each header element individually:

protected void Page_Load(object sender, EventArgs e)
{
    string[] tableHeaders = { // blah blah };

    foreach(var header in tableHeaders)
    {
        var tmp = new TableHeaderCell();
        tmp.ID = "TH_" + header;
        tmp.Text = header;
        headerRow.Cells.Add(tmp);
    }
}

Then when the page renders, it should render each one out as a <th> element within your row.

4 Comments

I like this answer, I get an error though. Error 3 Argument 1: cannot convert from 'System.Web.UI.WebControls.TableHeaderCell' to 'System.Web.UI.HtmlControls.HtmlTableCell' I probably need some using statements. (I'm still getting used to C#).
where does your variable headerRow come from ?
TableRow headerRow = new TableRow(); needs to be above your for each.
@Frantumn: No it doesn't. If you read the entire answer, you'll see that the headerRow is specified by the ID in the markup. That's why you need runat="server" as a part of the markup. Because headerRow is specified without any specific parent it should be available without using FindControl("headerRow"). If you have runat="server" specified on a parent element (such as table perhaps) then you would need to use FindControl.

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.