c#gridview行单击
当我点击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。
我有解决方案。
这就是我所做的:
if(e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'"; }
我已将前面的代码放在RowDataBound事件中。
马亭,
这是另一个带有一些漂亮的行突出显示和一个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键的空白查询字符串值:
因此,将第一列更改为:
将此css添加到页面顶部:
但要隐藏标题行的第一个单元格,请将其添加到代码隐藏中的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可以与gridview中显示的数据项相关吗?
如果是这样,您可以使用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,你的代码工作得很好,我添加了一点点黑客以避免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'; }");
如果有更好的方法,请告诉我,但它对我来说非常适合。
问候。