1

I disable a particular cell of my gridview on page load as follows

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].Enabled = false;

Now I would like to apply a javascript alert function for this cell

I tried this

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].Attributes.Add["onclick"]="f1();"

My function is as follows

<script type="text/javascript">
function f1() {
    alert('no more rows found to copy');
}

But didn't work for me ..

Arion
  • 31,011
  • 10
  • 70
  • 88
Developer
  • 8,390
  • 41
  • 129
  • 238
  • i think your attribute add method should look like this grdInvoice.Rows[grdInvoice.Rows.Count-1].Cells[6].Attributes.Add("onclick", "return f1();"); – Mac Apr 26 '12 at 07:01
  • I tried that too but didn't work for me – Developer Apr 26 '12 at 07:06
  • So if my understanding is right you want to execute a javascript function upon clicking the disabled gridview cell, please confirm! – Mac Apr 26 '12 at 07:09
  • Yeah on disabled cell I would like to apply – Developer Apr 26 '12 at 07:12
  • As you mention that you are doing these in page load can you try the same in gridview's row databound event in my case it works. – Mac Apr 26 '12 at 07:29

3 Answers3

1

you can set attributes like below.

grdInvoice.Rows[grdInvoice.Rows.Count - 1].Cells[6].Attributes.Add("disabled", "disabled");
grdInvoice.Rows[grdInvoice.Rows.Count - 1].Cells[6].Attributes.Add("onclick", "javascript:f1();");    

Try this ..hope this will helps you..

Prakash Patani
  • 547
  • 4
  • 8
0

You can use RowDataBound event. like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int? index = Session["LastIndex"] as int?;

        if (index != null && e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == index)
        {
            e.Row.Attributes["onclick"] = "f1()";
        }
    }

But you should have last index value in session.

I don't know how you are binding your grid but one option is this:

    List<ClassName> data = GetData(...);
    GridView1.DataSource = data;
    GridView1.DataBind();
    Session["LastIndex"] = data.Count - 1;
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
0

One thing you can try is You can assign a unique css class to that particular cell like this

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].CssClass = "className";

ad then execute a script after page load(refer to this link here here)

Using jQuery

$(document).ready(function(){ 
$(".className").live("click", function(){
alert('no more rows found to copy');
});
});
Community
  • 1
  • 1
Mac
  • 6,991
  • 8
  • 35
  • 67