繁体   English   中英

如何从GridView在新选项卡中打开页面?

[英]How to open page in new tab from GridView?

我想在单击gridview链接按钮时在新选项卡中打开页面。 但是我想根据警报类型打开新页面。 例如,从给定的下面的网格中,我单击Alert1的链接按钮,然后它应该打开alert1.aspx页面,如果它是Alert2然后是alert2.aspx 等帮助我找到合适的解决方案。 谢谢。

网格视图:

在此处输入图片说明

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

这是您要寻找的解决方案:

 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中的控件命名。

'_newtab'替换为'_blank'

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

您需要将target属性设置为_blank

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

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

将CommandName设置为警报类型并在click事件中对其进行访问

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM