2012-04-26 61 views
1

我在page load禁用我gridview的特定小区的JavaScript函数如下如何提醒在GridView的细胞

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

现在我想申请一个JavaScript警告功能,该单元

我想这

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

我的功能如下

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

但对我来说没有工作..

+0

我觉得你的属性添加方法应该是这样的 grdInvoice.Rows [grdInvoice.Rows.Count-1] .Cells [6]。 Attributes.Add(“onclick”,“return f1();”); – Mac 2012-04-26 07:01:38

+0

我也试过,但没有为我工作 – Dotnet 2012-04-26 07:06:08

+0

所以,如果我的理解是正确的,你想在点击禁用的gridview单元格后执行javascript函数,请确认! – Mac 2012-04-26 07:09:45

回答

1

你可以设置如下的属性。

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();");  

试试这个..hope这将帮助您..

+0

感谢普拉卡什..它的工作原理 – Dotnet 2012-04-26 08:09:15

0

可以使用RowDataBound事件。像这样:

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()"; 
     } 
    } 

但是你应该有会话中的最后一个索引值。

我不知道你是怎么结合你的网,但一个选项是这样的:

List<ClassName> data = GetData(...); 
    GridView1.DataSource = data; 
    GridView1.DataBind(); 
    Session["LastIndex"] = data.Count - 1; 
0

一件事你可以尝试是可以唯一的CSS类分配给特定的细胞像这样

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

广告然后执行页面加载脚本后(请参阅此链接在这里here

使用jQuery

$(document).ready(function(){ 
$(".className").live("click", function(){ 
alert('no more rows found to copy'); 
}); 
});