Ken Ray
Ken Ray

Reputation: 2528

Including eval / bind values in OnClientClick code

I have a need to open a popup detail window from a gridview (VS 2005 / 2008). What I am trying to do is in the markup for my TemplateColumn have an asp:Button control, sort of like this:

<asp:Button ID="btnShowDetails" runat="server" CausesValidation="false"
   CommandName="Details" Text="Order Details" 
   onClientClick="window.open('PubsOrderDetails.aspx?OrderId=<%# Eval("order_id") %>',
   '','scrollbars=yes,resizable=yes, width=350, height=550');"   

Of course, what isn't working is the appending of the <%# Eval...%> section to set the query string variable.

Any suggestions? Or is there a far better way of achieving the same result?

Upvotes: 7

Views: 22673

Answers (3)

EndangeredMassa
EndangeredMassa

Reputation: 17528

Do this in the code-behind. Just use an event handler for gridview_RowDataBound. (My example uses a gridview with the id of "gvBoxes".

Private Sub gvBoxes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBoxes.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim btn As Button = e.Row.FindControl("btnShowDetails")
            btn.OnClientClick = "window.open('PubsOrderDetails.aspx?OrderId=" & DataItem.Eval("OrderId") & "','','scrollbars=yes,resizable=yes, width=350, height=550');"
    End Select 
End Sub

Upvotes: 2

bdukes
bdukes

Reputation: 155915

I like @AviewAnew's suggestion, though you can also just write that from the code-behind by wiring up and event to the grid views ItemDataBound event. You'd then use the FindControl method on the event args you get to grab a reference to your button, and set the onclick attribute to your window.open statement.

Upvotes: 2

Tom Ritter
Tom Ritter

Reputation: 101330

I believe the way to do it is

onClientClick=<%# string.Format("window.open('PubsOrderDetails.aspx?OrderId={0}',scrollbars=yes,resizable=yes, width=350, height=550);", Eval("order_id")) %>

Upvotes: 13

Related Questions