如何从GridView中打开新标签页?

时间:2015-05-14 11:31:50

标签: c# asp.net gridview linkbutton

我想在点击gridview链接按钮的同时在新标签页中打开页面。但我想根据警报类型打开新页面。例如,从下面给出的网格中,我点击了Alert1的链接按钮,然后它应该打开 alert1.aspx 页面,如果它是Alert2,那么 alert2.aspx 。等等 帮我找一个合适的解决方案。谢谢。

GridView的:

enter image description here

<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);
}

4 个答案:

答案 0 :(得分:1)

以下是您要寻找的解决方案:

 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);
      }
 }

另外,为GridView中的控件指定唯一名称。

答案 1 :(得分:0)

'_newtab'替换为'_blank'

Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('alert1.aspx','_blank');", true);

答案 2 :(得分:-1)

您需要将target属性设置为_blank

Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open('alert1.aspx','_blank');", true);

window.open中的第二个参数是指定是要在新标签页还是现有标签页中打开页面。因此,您需要将其设置为_blank以在新标签页中打开页面

答案 3 :(得分:-1)

将CommandName设置为警报类型并在单击事件中访问它

 <asp:LinkButton ID="lnk" runat="server" Text="Click" OnClick="lnk_Click" CommandArgument='<%# Bind("Alert_Type") %>'>
</asp:LinkButton>

点击活动

protected void lnk_Click(object sender, EventArgs e)
{
string alerttype=e.CommandArgument.ToString();
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", "window.open("+alerttype+"'.aspx','_newtab');", true);
}