题
当我击中的行我的内,我要转到其他网页,与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);
}
我怎么能得到行一个独特的身份证(数据库),和当我击行,另一页开(如击href)和该网页可以读取身份证。
解决方案
我有解决方案。
这是我做了什么:
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
我的推杆在RowDataBound事件前面的代码。
其他提示
Martijn,
这里是另一个例子有一些漂亮的行突出显示和a 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 + "'");
}
}
代码以上的工作。净3.5.但是,你不能设置你的id列Visible="虚假",因为你会得到一个空白的查询串值为你的身份密钥:
<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;");
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事件。在您的按钮/链接,您wnat设置上点击,设置的CommandName和CommandArgument,您可以在活动方法的EventArgs的放慢参数accees。
在网格视图行点击重定向到其它页面
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,你的代码工作很细,我添加了一点点黑客以避免alternatingRowStyle鼠标移开后毁。
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'; }");
如果有更好的方式来做到这一点,请让我知道,但它的工作非常适合我。
问候。
不隶属于 StackOverflow