locked
Hyperlink open in another window RRS feed

  • Question

  • User-797751191 posted

    Hi

       I have below code and i want Url should open in another window.

    <asp:TemplateField HeaderText="Annexure" ItemStyle-width="50">
                                                            <ItemTemplate>
                                                                <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl='<%# Eval("Img1") %>' Text="View"  ControlStyle-CssClass="location"></asp:HyperLink>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>

    Thanks

    Tuesday, July 23, 2019 6:17 AM

Answers

  • User-719153870 posted

    Hi jsshivalik,

    You can use <%#Eval()%> to pass parameters to JS function.

    Please refer:

    ASPX:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function newwin(newurl) {
                window.open(newurl, "newwindow", "height=100, width=400,left=600,top=400")/*height and width set the size of the new window,left and top set the location of the new window from the upper left corner of the browser.*/
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" onclick='<%#Eval("uurl", "newwin(\"{0}\")")%>' runat="server" Text="View"></asp:HyperLink>
                    </ItemTemplate>
                </asp:Repeater>
                
            </div>
        </form>
    </body>
    </html>

    .CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
            public string connstring = "Data Source=.;Initial Catalog=ddd;Integrated Security=True";
    
            protected void GetData()
            {
                using (SqlConnection conn = new SqlConnection(connstring))
                {
                    conn.Open();
                    string sql = "select * from Urls";
                    SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Urls");
                    Repeater1.DataSource = ds;
                    Repeater1.DataBind();
                    conn.Close();
                }
            }

    Here's the result:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 24, 2019 1:26 AM

All replies

  • User-1038772411 posted

    Hello, jsshivalik

    Target property use to open new tab or same tab. the value "_blank" use to open hyperlink in new tab.

    <asp:HyperLink Target="_blank" />

    Thanks.

    Tuesday, July 23, 2019 6:31 AM
  • User-797751191 posted

    Hi AddWeb

      Thanks . Can we give size of window

    Thanks

    Tuesday, July 23, 2019 6:52 AM
  • User-719153870 posted

    Hi jsshivalik,

    If you want to change the size of the new window, i suggest you can use JS.

    Please refer to below codes:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function newwin() {
                window.open("https://www.google.com/", "newwindow", "height=100, width=400,left=600,top=400")/*newwindow is the name of the new window(it can be null),height and width set the size of the new window,left and top set the location of the new window from the upper left corner of the browser.*/
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:HyperLink ID="HyperLink1" onclick="newwin()" runat="server" Text="View"></asp:HyperLink>
            </div>
        </form>
    </body>
    </html>

    Here is the result of this demo:

    Best Regard,

    Yang Shen

    Tuesday, July 23, 2019 8:15 AM
  • User-797751191 posted

    Hi Yang

      U have hardcoded url www.google.com . I have Url saved in database , how i should pass

    Thanks

    Tuesday, July 23, 2019 9:23 AM
  • User-719153870 posted

    Hi jsshivalik,

    You can use <%#Eval()%> to pass parameters to JS function.

    Please refer:

    ASPX:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function newwin(newurl) {
                window.open(newurl, "newwindow", "height=100, width=400,left=600,top=400")/*height and width set the size of the new window,left and top set the location of the new window from the upper left corner of the browser.*/
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" onclick='<%#Eval("uurl", "newwin(\"{0}\")")%>' runat="server" Text="View"></asp:HyperLink>
                    </ItemTemplate>
                </asp:Repeater>
                
            </div>
        </form>
    </body>
    </html>

    .CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
            public string connstring = "Data Source=.;Initial Catalog=ddd;Integrated Security=True";
    
            protected void GetData()
            {
                using (SqlConnection conn = new SqlConnection(connstring))
                {
                    conn.Open();
                    string sql = "select * from Urls";
                    SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Urls");
                    Repeater1.DataSource = ds;
                    Repeater1.DataBind();
                    conn.Close();
                }
            }

    Here's the result:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 24, 2019 1:26 AM
  • User-1038772411 posted

    Hello, jsshivalik

    Yes we can. 

    Try this :

    <a href="http://www.facebook.com/sharer" onclick="window.open(this.href, 'mywin',
    'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); return false;" >Share this</a>

    User Hyperlink in your case instead of a tag.

    reference link :

    https://stackoverflow.com/questions/2541392/opening-new-window-in-html-for-target-blank

    https://stackoverflow.com/questions/44347535/new-window-size

    Thanks.

    Wednesday, July 24, 2019 4:59 AM