acadia
acadia

Reputation: 2623

Button in a GridView to open in a new Window(ASP.Net 3.5)

I have a gridview in my ASPX page. This GridView is inside a UpdatePanel which causes partial page postbacks.

I have a button as the last column for each row in the GridView. How do I open temp.aspx page with ID value passed to it onClick of each button. I have a ID column in the gridView too.

Please help

Upvotes: 1

Views: 3089

Answers (2)

rick schott
rick schott

Reputation: 20617

You can get the DataSource for each row OnRowDataBound, then find the control you want in that row and apply your OnClick JavaScript:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
          OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:TemplateField HeaderText="Example"
        <ItemTemplate>
        <asp:label ID="YourSpanToClick" runat="server" />
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
        Label yourSpanToClick= (Label)e.Row.FindControl("YourSpanToClick");

        YourDataSrcRowObject rowObject= DataBinder.Eval(e.Row.DataItem) as YourDataSrcRowObject ;
        string js = String.Format("window.open('{0}');", "temp.aspx?Id=" + rowObject.Id)
        yourSpanToClick.Attributes.Add("onclick",js);

     }

}

Upvotes: 1

Zaldy Baguinon
Zaldy Baguinon

Reputation: 96

You can't open an new window from OnClick event. Try the OnClientClick inside the RowDataBound event of that gridview.

Private Sub DataGridView1_OnRowDataBound(Sender as Object, e as DataGridView.EventArgs)
   Dim btn as Button=e.Row.FindControl("btnName")
   btn.OnClientClick="javascript:window.open(""temp.aspx?id=22"")
End Sub

Something like this.

Upvotes: 0

Related Questions