Search code examples
asp.netobout

Obout Grid: Hiding an Image on a grid for certain rows only


Using the obout grid with the following column:

<obg:Column ID="Image" DataField="" HeaderText="" Width="50" runat="server">
   <TemplateSettings TemplateId="ImageTemplate" />
</obg:Column>

And the following Template:

<Templates>
  <obg:GridTemplate runat="server" ID="ImageTemplate">
    <Template>
      <img src="images/test.png" title="test" />
    </Template>
  </obg:GridTemplate>
</Templates>

I am trying to hide the image on certain rows programmatically:

protected void grd_RowDataBound(object sender, GridRowEventArgs e)
{
    if (testpassed())
    {
        e.Row.Cells[1].Text = "";  // Column 2 is the image
    }
}

But it is not hiding the image. How do I hide the image programmatically using an obout grid for certain rows only? Thanks before hand.


Solution

  • If found the answer in case someone runs into this in the future:

    protected void grd_RowDataBound(object sender, GridRowEventArgs e)
    {
      // Check if this is a DataRow
      if (e.Row.RowType == GridRowType.DataRow)
      {
        // Check if we are hiding the image
        if (testpassed())
        {            
          // Retrieve Image Cell (Column 2 in my case)
          GridDataControlFieldCell cell = e.Row.Cells[1] as GridDataControlFieldCell;
    
          // Retrieve Literal Control with Image Source Html (Found at Level 5)
          LiteralControl imgTag = cell.Controls[0].Controls[0].Controls[0].Controls[0].Controls[0] as LiteralControl;
    
          // Remove Html <img src.. code from Literal Control in order to hide image
          imgTag.Text = "";
        }
      }
    }