I want to open page in new tab while clicking on the gridview link button. But I want to open new page based on alert type. For example, from the given below grid I clicked link button of Alert1 then it should open alert1.aspx
page, if it is Alert2 then alert2.aspx
. etc
Help me to find a proper solution. Thank you.
GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False">
<Columns>
<asp:TemplateField HeaderText="Alert Type" SortExpression="Alert_Type">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Alert_Type") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Alert_Type") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Created_Time" HeaderText="Created Time"
ReadOnly="True" SortExpression="Created_Time" />
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnk" runat="server" Text="Click" OnClick="lnk_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#:
protected void lnk_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('alert1.aspx','_newtab');", true);
}
Here's the solution that you looking for:
protected void lnk_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
Label Label1 = lnk.NamingContainer.FindControl("Label1") as Label;
if (Label1.Text == "Alert1")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('alert1.aspx','_blank');", true);
}
else if (Label1.Text == "Alert2")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('alert2.aspx','_blank');", true);
}
}
Also, Give unique names to controls inside GridView.