c#gridview行点击 [英] c# gridview row click
本文介绍了c#gridview行点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击 GridView 中的一行时,我想使用从数据库中获取的 ID 转到另一个页面.
When I click on a row in my GridView, I want to go to a other page with the ID I get from the database.
在我的 RowCreated 事件中,我有以下行:
In my RowCreated event I have the following line:
e.Row.Attributes.Add(
"onClick",
ClientScript.GetPostBackClientHyperlink(
this.grdSearchResults, "Select$" + e.Row.RowIndex));
为了防止错误消息,我有这个代码:
To prevent error messages i have this code:
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.
How can I give a row a unique ID (from the DB) and when I click the row, another page is opened (like clicking on a href) and that page can read the ID.
推荐答案
我有解决方案.
这就是我所做的:
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
我已将前面的代码放在 RowDataBound 事件中.
I have putted the preceding code in the RowDataBound event.
这篇关于c#gridview行点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文