views:

29

answers:

2

I have a table being generated in an ASP.net web app. The table has 4 columns. Currently each cell of each row can be clicked to get more detailed info. However, I do not want the first and the last cell in each row to be clickable. How can I make it so that only the first second and third cell can be clicked?

Below is some of the code (from the .cs file):

if(e.Row.RowType == DataControlRowType.DataRow)
         {
            e.Row.BackColor = TRADER_BACKCOLOR;
            e.Row.Cells[0].Font.Bold = true;
            e.Row.Attributes.Add("onmouseover", "style.backgroundColor = 'Silver'");
            e.Row.Attributes.Add("onmouseout", "style.backgroundColor = '" + TRADER_HEX + "'");
            e.Row.Attributes.Add("onclick", "RowClick(this, '" + e.Row.Cells[0].Text + "');");

From the .aspx file:

function RowClick(caller, id)
      {
         if(document.getElementById(id).style.display == "block")
         {
                if(last != "" && parent == id)
                {
                    HideDetailed();
                }
            document.getElementById(id).style.display = "none";
         }
         else
         {
            document.getElementById(id).style.display = "block";
         }
      }
+1  A: 

You'll have to know beforehand the count of whatever you are binding your table to for this to work.

// minus one since the RowIndex is 0-based
int total = whateverYouAreBindingTo.Count - 1;

then within your event or whatever it is where you're adding the OnClick event...

if (e.Row.RowIndex > 0 && e.Row.RowIndex < total)
   e.Row.Attributes.Add("onclick", "RowClick(this, '" + e.Row.Cells[0].Text + "');");
Jagd
A: 

I would just not add the attributes for the rows you don't want to be clicked, like:

if(e.Row.RowType == DataControlRowType.DataRow)
{

    e.Row.BackColor = TRADER_BACKCOLOR;
    e.Row.Cells[0].Font.Bold = true;
    e.Row.Attributes.Add("onmouseover", "style.backgroundColor = 'Silver'");
    e.Row.Attributes.Add("onmouseout", "style.backgroundColor = '" + TRADER_HEX + "'");
   if (e.Row.RowIndex != 0 && e.Row.RowIndex != yourGridView.Rows.Count - 1)
   {
     e.Row.Attributes.Add("onclick", "RowClick(this, '" + e.Row.Cells[0].Text + "');");
   }
EJC
Needs to be double ampersand, not the pipes though...
Jagd
Logically I think this is correct `if it's not 0 or it's not the last entry, then add the attribute`. I admit it's clumsy logic because I was typing off the top of my head, but I think it would work, no?
EJC
Let us assume that we are evaluating the first row, so the row index equals 0. According to your if statement, the first condition will evaluate as false and the second condition will evaluate as true. Since we have an **or** then only one of the conditions has to evaluate to true for the if block to be executed. This of course is not correct, as we don't want the first or last row to execute the if block.
Jagd
you're right, my fault. Sorry, I do that all the time, I should have known...
EJC