Sportalcraft
Sportalcraft

Reputation: 265

GridViewRow - OnClick

I want to redirect to another Page When I'm clicking a GridViewRow

I have tried this (with no success) :

[RowName].Attributes["onclick"] = "HttpContext.Current.Response.Redirect('[PageName].aspx')";

How to do that?

Thanks!

Upvotes: 1

Views: 73

Answers (2)

Sportalcraft
Sportalcraft

Reputation: 265

Thanks!

at first it didn't work, so I changed the code to this :

e.Row.Attributes.Add("onclick", "goUrl()");

<script>
function goUrl() {
    location.href = "[PageName].aspx";
}
</script>

Upvotes: 1

VDWWD
VDWWD

Reputation: 35564

You cannot bind a Response.Redirect to a onclick atrribute directly, only functions. But you cannot bind a function to a row (click). It's easier to use Javascript. It does not require a PostBack.

Bind the javascript onclick function to a row in the OnRowDataBound event of the gridview.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "goUrl('" + DataBinder.Eval(e.Row.DataItem, "url").ToString() + "')");
        }
    }

And then a JavaScript function.

<script type="text/javascript">
    function goUrl(url) {
        location.href = url;
    }
</script>

Upvotes: 1

Related Questions