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

本文关键字:asp net 视图 网格 单击 | 更新日期: 2023-09-27 18:37:02

我想要 asp.net 行可单击的网格视图。

我想在根据行索引单击该行时调用一个函数。

我尝试使用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);
        }
    }

我哪里出错了?

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

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

试试这个

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

你可以在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);
    }
}