views:

1016

answers:

3

I am dynamically creating a table which I want to have clickable rows. When the user clicks on one of the rows I want to redirect to a page specific to the item of that row. My question is how on the server side can I wire the "onclick" event to a routine that will then allow me to build a url based on some of the data included in the row they clicked?

for example I would want to do this on click:

Response.Redirect("SomePage.aspx?" itemType + "&" + COLUMN1VALUE);

where COLUMN1VALUE would be the first column in the row that was clicked.

+1  A: 

I would build the link in the table as an actual anchor tag to your SomePage.aspx (while you're building your table, you should know what the query string would be for each table row), opposed to a server side control that redirects on postback

John
This affords you less server side code, which I think is a good thing!
Zachary Yates
A: 

In your click event handler, you cast the sender as button, then get its parents until you get to the TableRow, then do a FindControl() to find the control with the value specific to that row and use it to build your URL!

Good luck!

Ricardo Villamil
+2  A: 

It sounds like your actual goal is simply to display/edit the row on another page. If this is the case, you could simply add a javascript event handler to the table row when you create it.

<tr onclick="window.location='DetailPage.aspx?id=<%= IdFromDb %>'">
    <!-- etc......-->
</tr>

If you use a GridView to create the table you can inject this in the RowDataBound event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

    string OnClickCmd = "window.location='DetailPage.aspx?id=";

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

        OnClickCmd += DataBinder.Eval(e.Row.DataItem, "IdFromDb").ToString() + "'";

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

    }
}

Unless you need to do something in the postback, there is no need to redirect. Further, you could just create a hyperlink when you create the row eliminating the need for javascript, you don't need the full row clicking experience.

HectorMac