views:

12356

answers:

8

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.

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);
    }

My question is, 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.

Thnx

A: 

Can your ID be related to the data item displayed in the gridview?

If so you can use e.Row.DataItem, and cast it to whatever type it is.

Andrew Bullock
A: 

I have the solution.

This is what i have done:

if(e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}

I have putted the preceding code in the RowDataBound event.

Id cast e.Row.DataItem, Eval is slow and doesn't allow refactoring to pick up the fact that you might have changed the ID field's name
Andrew Bullock
A: 

row click in grid view redirect to other page

    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 + "'";
        }
    }

works absolutely fine

+2  A: 
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 + "'";    
    }
}
Navigation through JS is bad, if it's not an internal site. Google won't index this.
Arnis L.
A: 

You can use the RowCommand event of the grid view for it. In your button/link where you wnat to set the click on, set the CommandName and CommandArgument, which you can accees at the EventArgs paramter of the event method.

Bhaskar
+1  A: 
JohnB
A: 

Thanks a lot.Works like charm

Tajas
A: 

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 + "'";
} }

mohan