可点击的网格视图行asp.net

时间:2012-06-13 05:53:59

标签: c# asp.net

我希望asp.net gridview的行可点击。

我希望在根据行索引单击该行时调用函数。

我试图使用RowDataBound事件,但它没有用,或者我

我使用了以下代码

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
        }
    }

我哪里错了?

我不想重定向到任何其他页面。 我想填写同一页面上的文本框中的值

2 个答案:

答案 0 :(得分:0)

您可以在javascript中创建一个函数,并从行的鼠标事件中调用它。

<强> Javacript

<script language="javascript" type="text/javascript"> 
    function setStyle(obj)
    {
       obj.style.cursor='hand';
       obj.style.textDecoration='underline';
    }

    function resetStyle(obj)
    {
       this.style.textDecoration='none'; 
    }
</script>

代码背后

protected void PeopleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onmouseover"] = "setStyle(this);";
        e.Row.Attributes["onmouseout"] = "resetStyle(this);";

        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
    }
}

答案 1 :(得分:0)

试试这个

 <script type="text/javascript" language="javascript">
        function call(id) {
            alert(id);
            // Do whatever
        }
    </script>

<asp:GridView ID="gvParent" DataKeyNames="ID" runat="server" PageSize="10" AllowPaging="true"
            PagerSettings-Mode="NextPrevious" AutoGenerateColumns="False" OnRowDataBound="gvParent_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Name" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField ID="HdnID" runat="server" Value='<%# Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

背后的代码

protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ID = ((HiddenField)e.Row.FindControl("HdnID")).Value;

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = "call(" + ID + ");";
        }
}