检查是否选择gridview的行或不使用JavaScript [英] Check if gridview row is selected or not with javascript

查看:145
本文介绍了检查是否选择gridview的行或不使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:我有一个,我已经实现了行选择包含JavaScript的ASP GridView的(我可以选择一行通过点击它的每一个细胞)。烧制事件的onclick通过JavaScript和我遇到的一个问题。当我尝试编辑该行的TE内容,通过排edit命令,此刻我在文本框点击关注吧,行单击事件再次被解雇,我失去了文本框的焦点。这样,我有我可以通过点击选择无处不在的GridView预期的效果,但我不能编辑行的内容。

I have the following situation: I have an asp gridview that I have implemented row selection with javascript (I can select a row by clicking on every cell of it). The fired event is onclick through javascript and I am encountering a problem. When I try to edit te content of that row, through row edit command, at the moment I click on the textbox to focus on it, the row click event is fired again and I lose focus of the textbox. This way I have the desired effect that I can select a gridview by clicking everywhere, but I cannot edit the contents of the row.

启用整行选择的背后code:

Enable full row select on code behind:

protected void gvMyQuizz_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.ToolTip = "Click to show the questions of the quiz";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvMyQuizz,"Select$"+e.Row.RowIndex);
        }
    }

最有帮助的事情是,如果我可以创建一个JavaScript函数,如果已经选择了被点击我的行,可以检查,这样我就可以返回false,否则选择$行,但我还没有发现太大的帮助在线。我需要通过JavaScript来完成这一点。任何帮助将是AP preciated。

The most helpful thing would be if I can create a javascript function that can check if the row that i being clicked is already selected, so I can return false, else select$row, but I haven't found much help online. I need to finish this through javascript. Any help would be appreciated.

更新

我已经改变了加到行上onclick事件属性

I have changed the attribute that is added to the row on the onclick event:

protected void gvMyQuizz_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.ToolTip = "Click to show the questions of the quiz";
                e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gvMyQuizz, "selectRow(" + e.Row.RowIndex+");");
            }
        }

使用Javascript:

Javascript:

function selectRow(index) {

            var $selected = $("#gvMyQuizz .alt");
            if ($selected.length == 0) {
                console.log('nothing');
            } else if ($selected.length == 1) {
                console.log('one');
            } else {
                console.log('problem');
            }
        }

这是我得到的是点击行时,但这发射只有一次一个功能,我不知道为什么:

A function that I get when the row is clicked, but this is fired only once, I don't know why:

$("[id*=gvMyQuizz] td").click(function () {
            console.log('clicked');
        });

时有人能找出什么是缺少能帮助解决这个问题。如果重复说我火点击时的功能,我可以检查是否有它通过这个<选定的或不href=\"http://forums.asp.net/t/1756884.aspx?How+to+check+whether+any+row+selected+or+not+in+gridview+by+jquery\"相对=nofollow>帖子。

推荐答案

编辑

如果你想处理行选择与你的onclick事件中,最简单的选项来检查你点击当前选中的行是否是保持在服务器运行结束的隐藏字段。这个字段将包含当前选择的行的索引。你仍然要触发回发到服务器,以更新像

If you want to handle row selection with your onclick event, the most straightforward option to check whether the row you have clicked is currently selected is to keep a hidden field running at the server end. This field would contain the index of the currently selected row. You would still have to trigger a post back to the server to update this value in your row selection logic with something like

<input id="inp_row_index" type="hidden" runat="server" />

function select(rowIndex) {
    document.getElementById('<%=inp_row_index.ClientID %>').value = rowIndex;
    <%=Page.GetPostBackEventReference(inp_row_index) %>
}

这基本上是在这里介绍的方法摘要:的http://www.$c$cproject.com/Tips/79822/How-to-handle-a-selected-row-in-GridView-on-the-cl

This is basically a summary of the approach described here: http://www.codeproject.com/Tips/79822/How-to-handle-a-selected-row-in-GridView-on-the-cl

这篇关于检查是否选择gridview的行或不使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆