当我点击GridView中的一行时,我想转到另一个页面,其中包含我从数据库中获取的ID。
在我的RowCreated事件中,我有以下一行:
e.Row.Attributes.Add(
"onClick",
ClientScript.GetPostBackClientHyperlink(
this.grdSearchResults, "Select$" + e.Row.RowIndex));
为防止出现错误消息,我有以下代码:
protected 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$" + i.ToString()));
}
// Do the standard rendering stuff
base.Render(writer);
}
如何为行添加唯一ID(来自数据库),当我单击该行时,会打开另一个页面(如点击href),该页面可以读取ID。
答案 0 :(得分:19)
我有解决方案。
这就是我所做的:
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
我已将前面的代码放在RowDataBound事件中。
答案 1 :(得分:19)
马亭,
这是另一个带有一些漂亮的行突出显示和一个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 =“false”,因为您将获得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;");
答案 2 :(得分:3)
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 + "'";
}
}
答案 3 :(得分:1)
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 + "'";
}
}
答案 4 :(得分:1)
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 + "'");
}
}
}
答案 5 :(得分:0)
您的ID可以与gridview中显示的数据项相关吗?
如果是这样,您可以使用e.Row.DataItem,并将其转换为任何类型。
答案 6 :(得分:0)
在网格视图中单击行重定向到其他页面
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 + "'";
}
}
工作得很好
答案 7 :(得分:0)
您可以使用网格视图的RowCommand事件。在您想要设置单击的按钮/链接中,设置CommandName和CommandArgument,您可以在事件方法的EventArgs参数处加入。
答案 8 :(得分:0)
JohnB,你的代码工作得非常好,我添加了一点点黑客以避免mouseout输出后的alternateRowStyle破坏。
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'; }");
如果有更好的方法,请告诉我,但这对我来说非常合适。
问候。