ユーザーが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 メソッドを使用して、検証のためにポストバックまたはコールバック データを登録します。