1

行をクリック可能な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);
        }
    }

どこが間違っていますか?

他のページにリダイレクトしたくありません。同じページのテキストボックスに値を入力したい

4

2 に答える 2

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 に答える
0

javascriptで関数を作って、行のマウスイベントから呼び出すことができます。

Javascript

<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 に答える