locked
Open new tab only if query returns data RRS feed

  • Question

  • User593583510 posted

    In my previous (resolved) post I described Page1.aspx with dropdownlist  and button which when clicked opened Page2.aspx in a new tab.

    In my project, button_click should open Page2.apsx only if a query returns data. If query result is null page1.aspx should be the only page open and allow to make different selection in the dropdownlist.

    I cannot figure out how to modify "SetTarget()" function to set target to "_blank" or to "_self", based on the condition of query result in button_click event.

    Below I copied code from my previous post with Boolean query result condition added in button_click event to possibly be used in the SetTarget() function.

    Can you please give me some direction how this can be implemented?

    Page1.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication2.Page1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type = "text/javascript">
            function SetTarget() {
                document.forms[0].target = "_blank";
            }
          window.onload = function () {
                document.getElementById("<%=DropDownList1.ClientID  %>")
                    .onchange = function () {
                     document.forms[0].target = "_self";
    __doPostBack("DropDownList1")
                    }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
             <div>
                Select Something in dropdownlist to populate Listbox<asp:DropDownList ID="DropDownList1" runat="server" Height="16px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="142px" AutoPostBack="True">
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>ddl item 1</asp:ListItem>
                    <asp:ListItem>ddl item 2</asp:ListItem>
                    <asp:ListItem>ddl item 3</asp:ListItem>
                </asp:DropDownList>
                <br />
                List Box Populated when dropdowlist idex changes&nbsp; <asp:ListBox ID="ListBox1" runat="server" Width="149px" SelectionMode="Multiple"></asp:ListBox>
                <br />
                Clicking &#39;Run report&#39; button opens Page2 in new tab
                <asp:Button ID="btnRunReport" runat="server" Text="Run Reprot" OnClick="btnRunReport_Click" OnClientClick = "SetTarget();" />
            </div>
        </form>
    </body>
    </html>

    Page1.aspx.cs:

    using System;
    using System.Collections.Generic;
    
    namespace WebApplication2
    {
        public partial class Page1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                ListBox1.Items.Clear();
                switch (DropDownList1.SelectedValue)
                {
                    case "ddl item 1":
                        {
                            ListBox1.Items.Add("list item 1");
                            ListBox1.Items.Add("list item 2");
                            ListBox1.Items.Add("list item 3");
                            break;
                        }
                    case "ddl item 2":
                        {
                            ListBox1.Items.Add("list item 4");
                            ListBox1.Items.Add("list item 5");
                            ListBox1.Items.Add("list item 6");
                            break;
                        }
    
                }
            }
    
            protected void btnRunReport_Click(object sender, EventArgs e)
            {
                List<string> lst = new List<string>();
                foreach (int n in ListBox1.GetSelectedIndices())
                {
                    lst.Add(ListBox1.Items[n].Value);
                }
    			
    			Boolean result = false;
    			
    			//lst is used in SQL Server query that returns data to be viewed on page2
    			//query result loaded into a DataTable
    			
    			if (DataTable.Rows.Count < 1) 
    			{
    				result = false;
    				//do not open Page2 in a new tab, stay on Page1
    			}
    			else
    			{
    				result=true;
    				Context.Items["reportlist"] = lst;  //for demonstration lst is passed instead of DataTable
    				Server.Transfer("Page2.aspx");
    				//Open page2 in new tab
    			}
                
            }
        }
    }

    Page2.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs" Inherits="WebApplication2.Page2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="lblReportData" runat="server" Text=""></asp:Label>
            </div>
        </form>
    </body>
    </html>

    Page2.aspx.cs:

    using System;
    using System.Collections.Generic;
    
    namespace WebApplication2
    {
        public partial class Page2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                List<string> replst = new List<string>();
                replst = (List<string>)Context.Items["reportlist"];
                foreach(string m in replst)
                {
                    lblReportData.Text += m + "<br/>";
                }
    
            }
    
           
        }
    }

    Thursday, October 25, 2018 8:04 PM

All replies

  • User753101303 posted

    Hi,

    You can't check that when building your drop down ? It would seem better to let the user know which values would produce no result anyway (they could be disabled or dimmed).

    At worst using Ajax and caching the result so that you can then take the decision about what to do next. It's likely too late when the next page is already triggered. Windows are handled on the client side only so you have no direct control on this from the server side.

    Thursday, October 25, 2018 11:34 PM
  • User-1716253493 posted

    I have try to change the redirect target by setting form target like below and work

                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem>_self</asp:ListItem>
                    <asp:ListItem>_blank</asp:ListItem>
                </asp:DropDownList>
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            form1.Target = DropDownList1.Text; //work
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("page2.aspx");
        }

    But it will not work if you set the target just before redirect

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            form1.Target = DropDownList1.Text; //not work
            Response.Redirect("page2.aspx");
        }

    Friday, October 26, 2018 1:17 AM
  • User839733648 posted

    Hi Archy0001,

    I suggest that you could add an if-statement to check if the data in the list is null.

    If it is null, just reload the page. And if it is not null, just turn to page2.

    For more, you could refer to the code below.

            protected void btnRunReport_Click(object sender, EventArgs e)
            {
                List<string> lst = new List<string>();
                foreach (int n in ListBox1.GetSelectedIndices())
                {
                    lst.Add(ListBox1.Items[n].Value);
                }
                if (lst.Count() == 0)
                {
                    Response.Redirect(Page.Request.RawUrl, false);
                }
                else
                {
                    Context.Items["reportlist"] = lst;
                    Server.Transfer("Page2.aspx");
                }
                
            }

    Best Regards,

    Jenifer

    Tuesday, October 30, 2018 7:38 AM