通过datagridView重定向
本文关键字:重定向 datagridView 通过 | 更新日期: 2023-09-27 18:21:31
我目前在ASP.Net/C#工作。我正试图通过数据网格中的行使服务器重定向到另一个页面,然而,我的代码似乎没有工作。如有任何帮助,我们将不胜感激。
protected void GridView1_RowCreated()
{
foreach (GridViewRow row in GridView1.Rows)
{
Color color = row.BackColor;
string color2 = System.Drawing.ColorTranslator.ToHtml(color);
Session["" + row.Cells[0].Text + ""] = row.Cells[0].Text;
//row.Attributes.Add("onclick", "zz(); return false;");
/*if (row.RowState == DataControlRowState.Alternate)
{
row.Attributes.Add("onclick", "redirectFunction(); return false;");
//row.Attributes.Add("onmouseover", "this.style.backgroundColor=' #FFFFFF';");
//row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + color2 + "';");
//row.Attributes.Add("onclick", cookie.Value = row. GridView1.SelectedRow.Cells[0].Text);
//row.Attributes.Add("onclick", "zz(); return false;");
}
else
{
//row.Attributes.Add("onmouseover", "this.style.backgroundColor=' #FFFFFF';");
//row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + color2 + "';");
//row.Attributes.Add("onclick", "zz(); return false;");
}*/
}
}
public void redirectFunction()
{
Response.RedirectPermanent("View.aspx");
}
Other page
if(Session["session"].ToString() != null)
{
// do something
}
看起来您添加了一个客户端点击事件,然后试图调用一个无法工作的c#方法。
此代码:
foreach (GridViewRow row in GridView1.Rows)
{
row.Attributes.Add("onclick", "redirectFunction()");
// Or if you want to pas in an id
row.Attributes.Add("onclick", "redirectFunction(" + rowId + ")");
}
将一个javascript点击事件添加到行中,并告诉它调用一个javascript方法。然后,您需要将javascript方法添加到您的aspx页面:
<script type="text/javascript">
function redirectFunction() {
alert("do you see this??");
window.location = "View.aspx";
}
</script>
我添加了一个警告语句,你可以去掉它,但你看到这个弹出窗口了吗?如果没有,请检查页面上是否存在任何javascript错误。
你也可以用jQuery(它更容易):
$('.yourrowclassname).click(function () {
window.location = "View.aspx";
});
这意味着您不需要在代码中添加属性,只需使用/向行中添加一个类即可。
您应该通过使用GridView的SelectedIndexChanged事件来实现此功能。从那里,您可以获取需要在View.aspx页面中显示的对象的所有属性和信息。
在这里查看这篇文章,这将帮助你实现你想要做的事情https://stackoverflow.com/a/331662/125551
只需添加session["var"]=val或在url中发送该值,然后从querystring而不是session 中在其他页面的page_load中获取该值