Asp.net 如何在gridview中警告单元格上的javascript函数

Asp.net 如何在gridview中警告单元格上的javascript函数,asp.net,gridview,Asp.net,Gridview,我在页面加载上禁用我的网格视图的特定单元格,如下所示 grdInvoice.Rows[grdInvoice.Rows.Count-1] .Cells[6].Enabled = false; <script type="text/javascript"> function f1() { alert('no more rows found to copy'); } 现在我想为这个单元格应用一个javascript警报函数 我试过这个 grdInvoice.

我在
页面加载
上禁用我的
网格视图
的特定单元格,如下所示

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].Enabled = false;
<script type="text/javascript">
function f1() {
    alert('no more rows found to copy');
}
现在我想为这个单元格应用一个javascript警报函数

我试过这个

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].Attributes.Add["onclick"]="f1();"
我的职责如下

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].Enabled = false;
<script type="text/javascript">
function f1() {
    alert('no more rows found to copy');
}

函数f1(){
警报(“没有找到更多要复制的行”);
}


但对我不起作用。

您可以使用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;
List data=GetData(…);
GridView1.DataSource=数据;
GridView1.DataBind();
会话[“LastIndex”]=data.Count-1;

您可以使用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;
List data=GetData(…);
GridView1.DataSource=数据;
GridView1.DataBind();
会话[“LastIndex”]=data.Count-1;

您可以尝试的一件事是,您可以像这样为特定单元格分配一个唯一的css类

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].CssClass = "className";
ad然后在页面加载后执行脚本(请参阅此处的链接)

使用jQuery

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

您可以尝试的一件事是,您可以像这样为特定的单元格分配一个唯一的css类

grdInvoice.Rows[grdInvoice.Rows.Count-1]
          .Cells[6].CssClass = "className";
ad然后在页面加载后执行脚本(请参阅此处的链接)

使用jQuery

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

您可以像下面这样设置属性

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

试试这个..希望这对你有帮助..

你可以设置如下属性

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

试试这个..希望这对您有所帮助..

我认为您的属性添加方法应该类似于grdInvoice.Rows[grdInvoice.Rows.Count-1].Cells[6].Attributes.add(“onclick”,“return f1();”;我也试过了,但对meSo不起作用。如果我的理解是正确的,您希望在单击禁用的gridview单元格时执行javascript函数,请确认!是的,在禁用的单元格上,我想应用当您提到您正在页面加载中执行这些操作时,您可以在gridview的行数据绑定事件中尝试同样的操作吗?在我的情况下,它可以工作。我认为您的属性添加方法应该类似于grdInvoice.Rows[grdInvoice.Rows.Count-1]。单元格[6]。Attributes.add(“onclick”,“return f1();”;我也试过了,但对meSo不起作用。如果我的理解是正确的,您希望在单击禁用的gridview单元格时执行javascript函数,请确认!是的,在禁用的单元格上,我想应用,当你提到你正在页面加载中执行这些操作时,你可以在gridview的行数据绑定事件中尝试同样的操作吗?在我的情况下,它可以工作。