0

For some reason in the C# code behind for asp.net, i cannot call the table by it's ID to set it's back color property. I tried and and nothing seems to work

Here is my asp.net table tag:

<table id="ptbl" runat="server" cellpadding="2" width="640px" border="1">

here is something similar to what i what to do in the C# code behind but it does not recognize the id

ptbl.Attributes.Add("style", "background-color:red")");

Any ideas/suggestions?

Update: here is the code. There is a layout template in it so somehow it can't see the table id, but if I take that out of it then it sees it. What can i do. I need the listview to get the data

  <asp:ListView ID="ListView1" runat="server" Style="color: white; font-weight: bold">
        <LayoutTemplate>
            <table id="ptbl" runat="server" cellpadding="2" width="640px" border="1" style="color: black; font-weight: bold">
                <tr runat="server">
                    <th runat="server">Ps</th>
                    <th runat="server">P</th>
                    <th runat="server">T</th>
                </tr>
                <tr runat="server" id="itemPlaceholder" />
            </table>
            <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="90">
                <Fields>
                    <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true"
                        FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                        NextPageText=" &gt; " PreviousPageText=" &lt; " />
                </Fields>
            </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
            <tr runat="server">
                <td>
                    <asp:Label ID="Label1" runat="server"><%# Eval("Ps") %></asp:Label></td>
                <td>
                    <asp:Label ID="Label2" runat="server"><%# Eval("P") %></asp:Label></td>
                <td>
                    <asp:Label ID="Label3" runat="server"><%# Eval("T") %></asp:Label></td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

Again the layout template is causing the issue. how can i use the listview and the table with the layout template. I know this has morphed a bit now , but any help would be appreciated.

user3240928
  • 525
  • 2
  • 4
  • 16
  • is ptb1 not recognizable in codebehind? – Akash KC Jul 13 '17 at 01:08
  • @AkashKC - correct ptbl (L not a 1) is not being recognized in the code behind – user3240928 Jul 13 '17 at 01:16
  • It's recognized in my codebehind. but you have compiler error in adding attribute. it should be like `ptbl.Attributes.Add("style", "background-color:red");` – Akash KC Jul 13 '17 at 01:20
  • ok, i see what happened but it does not solve my problem if i add an id to the layouttemplate below. see edit above: – user3240928 Jul 13 '17 at 01:42
  • 3
    Possible duplicate of [How to access controls in listview's layouttemplate?](https://stackoverflow.com/questions/48616/how-to-access-controls-in-listviews-layouttemplate) – Jon P Jul 13 '17 at 01:56

1 Answers1

0

You cannot access the ptbl table until DataBind() has been called on ListView1. After it has been called and there is data in the dataset, then you can reference the the ptbl table like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        var tbl = new DataTable();
        tbl.Columns.Add("Ps", typeof(Int32));
        tbl.Columns.Add("P", typeof(string));
        tbl.Columns.Add("T", typeof(string));

        var r = tbl.NewRow();
        r[0] = 99;
        r[1] = "Hey";
        r[2] = "USA";
        tbl.Rows.Add(r);
        ListView1.DataSource = tbl;
        ListView1.DataBind();

        var ptbl = (HtmlTable)ListView1.FindControl("ptbl");
        ptbl.Style.Add("background-color", "red");
    }
Gregg
  • 615
  • 6
  • 6