クリックしたときに別のページにリダイレクトしたいGridViewRow
私はこれを試しました(成功しませんでした):
[RowName].Attributes["onclick"] = "HttpContext.Current.Response.Redirect('[PageName].aspx')";
どうやってするか?
ありがとう!
クリックしたときに別のページにリダイレクトしたいGridViewRow
私はこれを試しました(成功しませんでした):
[RowName].Attributes["onclick"] = "HttpContext.Current.Response.Redirect('[PageName].aspx')";
どうやってするか?
ありがとう!
Response.Redirect を onclick 属性に直接バインドすることはできません。関数のみです。ただし、関数を行にバインドすることはできません (クリック)。Javascript を使用する方が簡単です。PostBack は必要ありません。
OnRowDataBound
グリッドビューのイベントで、JavaScript の onclick 関数を行にバインドします。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "goUrl('" + DataBinder.Eval(e.Row.DataItem, "url").ToString() + "')");
}
}
そして、JavaScript 関数。
<script type="text/javascript">
function goUrl(url) {
location.href = url;
}
</script>
ありがとう!
最初はうまくいかなかったので、コードを次のように変更しました。
e.Row.Attributes.Add("onclick", "goUrl()");
<script>
function goUrl() {
location.href = "[PageName].aspx";
}
</script>