locked
Can you reference a table inside of a gridview item template? RRS feed

  • Question

  • User-985624828 posted

    I have a programming issue that I can’t seem to resolve.

    Picture this:  you have a gridview control that contains a table in the item template.  I would like to make the height of the first row zero during the Gridview.Rowdatabound event when certain conditions are met, but I can’t seem to reference either the table or the row.  You can give them ID’s but you can’t use findcontrol to reference them.  Is what I want to do possible?  How can I reference the table row (not the gridview row) in this event?

    Sunday, August 7, 2016 9:15 PM

Answers

  • User-985624828 posted

    Never mind, found "colgroup."  I can now finally do exactly what I set out to do.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 11:34 PM

All replies

  • User-271186128 posted

    Hi artsnob,

    According to your description, I think it will be an effective solution to add "runat='Server'" property to the table or the row you want to reference. Any element marked with "runat='Server'" lets the framework know that this will be a control on the server. Then, you could use FindControl() method to reference them.

    You could refer the following code:

      <asp:GridView ID="GridView1" DataKeyNames = "CustomerId" runat="server" AutoGenerateColumns = "false" OnRowEditing = "EditCustomer" OnRowDataBound = "RowDataBound" OnRowUpdating = "UpdateCustomer" OnRowCancelingEdit = "CancelEdit">
    <Columns>
        <asp:BoundField DataField = "ContactName" HeaderText = "Contact Name" />
        <asp:TemplateField HeaderText = "City">
        <ItemTemplate>
            <table id="table">
                <tr style="border:solid 1px " id="row1" runat="server"><td><b>customer city</b></td></tr>
                <tr style="border:solid 1px "><td><asp:Label ID="lblCity" runat="server" Text='<%# Eval("City")%>'></asp:Label></td></tr>
            </table>    
        </ItemTemplate>
        <EditItemTemplate>
                <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City")%>' Visible = "false"></asp:Label>
        <asp:DropDownList ID = "ddlCities" runat = "server">
        </asp:DropDownList>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" /> 
    </Columns>
    </asp:GridView>
    

    Code Behind:

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lb = e.Row.Cells[1].FindControl("lblCity") as Label;
                    HtmlTableRow dr = (HtmlTableRow)e.Row.FindControl("row1");
                    string city = lb.Text;
                    if (city == "London")
                    {
                        dr.BgColor = "Green";
                        dr.Height = "50px";
                    }
                    else
                    {
                        dr.Height = "0px";
                    }
                }      
            }
    

    Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

    Best regards,
    Dillion

    Monday, August 8, 2016 6:13 AM
  • User-1716253493 posted

    Add runat="server" to the table or row

        <table id="table1" runat="server">
            <tr id="row1" runat="server">
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>

    use findcontrol in rowdatabound event

            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim row1 As HtmlTableRow = e.Row.FindControl("row1")
                Dim table1 As HtmlTable = e.Row.FindControl("table1")
            End If

    Monday, August 8, 2016 7:01 AM
  • User-985624828 posted

    Thanks to both of you very much for your prompt and informed replies.  You solved the referencing issue -- I was using Tablerow instead of HtmlTableRow.

    Unfortunately, I cannot achieve my ultimate goal -- it seems that height is a read-only property with HTMLTableRows.  My ultimate goal was to collapse the first row of the table and have the other rows move up when a certain condition was met, but it seems that this cannot be done.  At least now I know.  Thanks again.

    Monday, August 8, 2016 2:48 PM
  • User-985624828 posted

    Please see my other reply in this thread.

    Monday, August 8, 2016 2:49 PM
  • User-985624828 posted

    Addendum:  I was finally able to make the lines move up by setting visible to false rather than playing around with cell height.

    But a new problem ensued:  That first line also controls the column widths of the table, and if you make it invisible, the cells below it no longer format properly.

    The line I'm trying to conditionally eliminate is like this:


    <tr id="SchedRow" runat="server">
     <td align="left" colspan="1" width="55" class="auto-style1">
             <asp:Label ID="lblStrt" runat="server" Text='<%# Trim(Right(Eval("strScreening").ToString, 8)) %>' Font-Size="8pt" BorderStyle="Solid" BorderWidth="5px" Font-Bold="True" Width="55px"></asp:Label>
     </td>
     <td align="left" colspan="1" width="192" class="auto-style1">
             <asp:Label ID="lblVenue" runat="server" Text='<%# Trim(Eval("strVenue").ToString)%>' Font-Size="8pt" BorderStyle="Solid" BorderWidth="5px" Font-Bold="True" Width="155px"></asp:Lab</t<td align="right" colspan="1" width="43" class="auto-style1">
     </td>
    </tr>                   

    The widths affect the positioning of cells on lines below it., some of which contain colspan=2 settings.  I experimented with a dummy row, with a height of 1, but got nowhere.  Do you happen to know of a way that column widths can be designated without using the first row?

    .

    <asp:label id="lblStrt" width="55px" font-bold="True" borderwidth="5px" borderstyle="Solid" bordercolor="#CC3300" forecolor="White" backcolor="#CC3300" font-size="8pt" text="<%# Trim(Right(Eval("strScreening").ToString, 8)) %>" runat="server"></asp:label><asp:label id="lblVenue" width="155px" font-bold="True" borderwidth="5px" borderstyle="Solid" bordercolor="#CC3300" forecolor="White" backcolor="#CC3300" font-size="8pt" text="<%# Trim(Eval("strVenue").ToString)%>" runat="server"></asp:label>

    Monday, August 8, 2016 11:16 PM
  • User-985624828 posted

    Never mind, found "colgroup."  I can now finally do exactly what I set out to do.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 11:34 PM