1

我想要其行可点击的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);
        }
    }

我哪里错了?

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

4

2 回答 2

0

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

Java脚本

<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);
    }
}
于 2012-06-13T05:57:47.810 回答
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 + ");";
        }
}
于 2012-06-13T06:03:20.970 回答