locked
How to keep styling on ListView Tableheader Link (after programmatically setting value) RRS feed

  • Question

  • User-642568311 posted

    I would like to change the text on a particular table header in a Layout Template in a ListView programmatically (depending on the value in the database). It works but the styling is no longer using the LinkButton styling(green and clickable). Instead it's black and bold (not clickable)

    I used the following (https://stackoverflow.com/questions/14385325/access-th-in-listview):

    My c#:

    HtmlTableCell thCustCol = lstAuthorizations.FindControl("customColumn") as HtmlTableCell;
    thCustCol.InnerText = query.Select(x => x.Custom1).First().ToString();

    I also tried applying FindControl to "LinkButton10" instead, but then it had an (null reference)exception.

    My aspx:

    <LayoutTemplate>
    <div class="table-responsive">
    <table class="table">
    <thead>
    <tr>
    <th runat="server" id="customColumn"><asp:LinkButton id="LinkButton10" runat="server" CommandArgument="Custom1" CommandName="Sort">Group ID</asp:LinkButton></th>

    What am I missing?

    Wednesday, December 12, 2018 10:54 PM

Answers

  • User-1174608757 posted

    Hi understandcsharp:

    According to your description, I have made the sample here. HtmlTableCell thCustCol = ListView1.FindControl("customColumn") as HtmlTableCell will get the cell which contains the linkbutton you want to keep styling.The code thCustCol.InnerText = query.Select(x => x.Custom1).First().ToString() will change the contents of the cell to a text which means the linkbutton would be deleted.I guess what you want is to change the text of linkbotton,but the code you post would delete linkbutton and add a text.Here is my code.I hope it will hep you.

    LinkButton.aspx:

    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ListView ID="ListView1" runat="server">
                    <LayoutTemplate>
                 <div class="table-responsive">
                    <table class="table">                  
                        <thead>
    <tr>
    <th runat="server" id="customColumn">
        <asp:LinkButton id="LinkButton1" runat="server" CommandArgument="Custom1" CommandName="Sort">Group ID</asp:LinkButton>
    </th>
    </tr>
                          <tr id="itemPlaceholder" runat="server">      
                          </tr>
                        </thead>
    
                    </table>
                 </div>
                    </table>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <tr runat="server">       
                     
                <td>
                  <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("id") %>' />
                </td>
                <td>
               <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("username") %>' />
                </td>
              </tr>
                    </ItemTemplate>
                  
                  
                </asp:ListView>
    
               </div>
        </form>
    </body>

    LinkButton.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!IsPostBack)
                {
                    string sql = "select * from db";
                    this.ListView1.DataSource = SqlHelper.ExecuteDataTable(sql);
                    ListView1.DataBind();
                    HtmlTableCell thCustCol = ListView1.FindControl("customColumn") as HtmlTableCell;
                    LinkButton lk = thCustCol.FindControl("LinkButton1") as LinkButton;
                    lk.Text = "link text";
                }
            }

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 14, 2018 9:42 AM