30

ユーザーがGridViewの行の任意のポイントを押すと、選択ボタンの代わりに行が選択される機能を実装しています。

ここに画像の説明を入力

それを実装するために、次のコードを使用しています。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Set the hand mouse cursor for the selected row.
        e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

        // The seelctButton exists for ensuring the selection functionality
        // and bind it with the appropriate event hanlder.
        LinkButton selectButton = new LinkButton()
        {
            CommandName = "Select",
            Text = e.Row.Cells[0].Text
        };

        e.Row.Cells[0].Controls.Add(selectButton);
        e.Row.Attributes["OnClick"] =
             Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
    }
}

上記のコードでは、次の問題があります。

  • これEnableEventValidationは、ページの I が に設定されている場合にのみ正常に機能しますfalse
  • は、ページに対してが呼び出されSelectedIndexChangedた場合にのみ発生します (すべてのポストバックで)。Grid.DataBind()Page_Load

私は何か間違ったことをしていますか?より良い実装はありますか?


編集:EnableEventValidationが に設定されている 場合true、次のエラーが表示されます。

ポストバックまたはコールバック引数が無効です。イベントの検証は、設定で使用するか、ページで <%@ Page EnableEventValidation="true" %> を使用して有効にします。セキュリティ上の目的で、この機能は、ポストバック イベントまたはコールバック イベントへの引数が、それらを最初にレンダリングしたサーバー コントロールから発信されていることを確認します。データが有効で期待される場合は、ClientScriptManager.RegisterForEventValidation メソッドを使用して、検証のためにポストバックまたはコールバック データを登録します。

4

5 に答える 5

46

データバインディングだけでなく、すべてのポストバックにこれを追加する必要があります。したがって、GridView のRowCreated -Event を使用する必要があります。

例えば

(C#):

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "Click to select row";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}

(VB.Net):

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
        e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
        e.Row.ToolTip = "Click to select row"
        e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
    End If
End Sub
于 2011-06-06T10:42:09.993 に答える
13

で行う代わりに、で行うことRowCreatedもできますRender()。そうすれば、GetPostBackClientHyperlinkwith true onのオーバーロードを使用してregisterForEventValidation、「無効なポストバック/コールバック引数」エラーを回避できます。

このようなもの:

protected override void Render(HtmlTextWriter writer)
    {
      foreach (GridViewRow r in GridView1.Rows)
      {
        if (r.RowType == DataControlRowType.DataRow)
        {
          r.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
          r.Attributes["onmouseout"] = "this.style.textDecoration='none';";
          r.ToolTip = "Click to select row";
          r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + r.RowIndex,true);

        }
      }

      base.Render(writer);
    }
于 2013-03-27T17:16:21.343 に答える
3
<style type="text/css">
    .hiddenColumn
    {
        display: none;
    }

    .rowGrid
    {
        cursor: pointer;
    }
</style>

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="true" >
            <RowStyle CssClass="rowGrid" />
            <Columns>
                <asp:CommandField ButtonType="Button" ShowSelectButton="true" HeaderStyle-CssClass="hiddenColumn"
                    ItemStyle-CssClass="hiddenColumn" FooterStyle-CssClass="hiddenColumn" />
            </Columns>
        </asp:GridView>

<script type="text/javascript">
        $(function () {
            $("#<%= GridView1.ClientID %> tr.rowGrid")
            .live("click", function (event) {
                $(this).find("input[type='button'][value='Select']").click();
            });

            $("#<%= GridView1.ClientID %> input[type='button'][value='Select']")
                .live("click", function (event) {
                    event.stopPropagation();
                });

        });
    </script>
于 2011-06-06T10:49:08.277 に答える
0

これを試して、グリッドに OnSelectedIndexChanged イベントを追加してください

   OnSelectedIndexChanged="Grid_SelectedIndexChanged"

そしてコードビハインド

 protected void Grid_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = gvSummary.SelectedRow;
    //Int32 myvalue= Convert.ToInt32(row.Attributes["ColumnName"].ToString());
   } 

EnableViewState="false" を設定しますが、ここでは、コードで既に行っていることをさらに 2 つ実行する必要があります。これは、EnableEventValidation を false に設定し、ページの Load で Grid Databinding を設定することを意味します。

于 2011-06-06T10:56:55.347 に答える
-2

ASPXコードで使用<asp:LinkButton>してみて<tr>、LinkBut​​tonのCommandName='Select'And in itemコマンドイベントを設定し、このコマンドを処理して、選択した行のスタイルを設定してください。

于 2011-06-06T10:31:53.570 に答える