質問
GridViewの行をクリックすると、データベースから取得したIDを持つ他のページに移動したい。
RowCreatedイベントには次の行があります:
e.Row.Attributes.Add(
"onClick",
ClientScript.GetPostBackClientHyperlink(
this.grdSearchResults, "Selectprotected override void Render(HtmlTextWriter writer)
{
// .NET will refuse to accept "unknown" postbacks for security reasons.
// Because of this we have to register all possible callbacks
// This must be done in Render, hence the override
for (int i = 0; i < grdSearchResults.Rows.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(
new System.Web.UI.PostBackOptions(
grdSearchResults, "Select<*>quot; + i.ToString()));
}
// Do the standard rendering stuff
base.Render(writer);
}
quot; + e.Row.RowIndex));
エラーメッセージを防ぐには、次のコードを使用します:
<*>行に一意のID(DBから)を付与し、行をクリックすると別のページが開き(hrefをクリックするなど)、そのページでIDを読み取ることができます。
解決
解決策があります。
これは私がやったことです:
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
前のコードをRowDataBoundイベントに入れました。
他のヒント
Martijn、
気の利いた行の強調表示とhrefスタイルのカーソルを使用した別の例を次に示します。
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
}
}
上記のコードは.NET 3.5で機能します。ただし、id列をVisible =&quot; false&quot;に設定することはできません。 IDキーの空のクエリ文字列値を取得するため:
<asp:GridView ID="gvSearch" runat="server" OnRowDataBound="gvSearch_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" Visible="false" />
<asp:BoundField DataField="first_name" HeaderText="First" />
<asp:BoundField DataField="last_name" HeaderText="Last" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="state_name" HeaderText="State" />
</Columns>
</asp:GridView>
したがって、代わりに最初の列をこれに変更します:
<asp:BoundField DataField="id" ItemStyle-CssClass="hide" />
このCSSをページの上部に追加します:
<head>
<style type="text/css">
.hide{
display:none;
}
</style>
<head>
ただし、ヘッダー行の最初のセルを非表示にするには、コードビハインドでgvSearch_RowDataBound()に追加します:
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hide";
}
明らかに、コードビハインドでid列を非表示にすることもできますが、これにより、cssクラスよりも多くのテキストがマークアップに表示されます。
e.Row.Cells[0].Attributes.Add("style", "display:none;");
e.Row.Attributes.Add("style", "cursor:pointer;");
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow gvr = e.Row;
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
gvr.Attributes.Add("OnClick", "javascript:location.href='Default.aspx?id=" + abc + "'");
}
}
}
IDはグリッドビューに表示されるデータ項目に関連付けることができますか?
もしそうなら、e.Row.DataItemを使用し、それをどんな型にもキャストできます。
グリッドビューのRowCommandイベントを使用できます。クリックを設定するボタン/リンクで、CommandNameとCommandArgumentを設定します。これらは、イベントメソッドのEventArgsパラメーターでアクセスできます。
グリッドビューで行をクリックして他のページにリダイレクトする
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
まったく問題なく動作します
JohnB、あなたのコードは非常にうまく機能します。マウスアウト後のRowRowStyleの変更を避けるために、ちょっとしたハックを追加しました。
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
変更先:
e.Row.Attributes.Add("onmouseout", "if(" + e.Row.RowIndex + "% 2 == 0) { this.style.backgroundColor=''; } else { this.style.backgroundColor = '#E8F7EA'; }");
もっと良い方法があれば教えてください。しかしそれは私にとって完璧に機能しています。
挨拶。