Popup Blocker issue in <Asp:Treeview....>

I am working on a solution where I have an element <Asp:Treeview....>

Now it does " ScriptManager.RegisterStartupScript()" at the back end when the Child node is selected for opening a new window. Even though this is a Client click (Direct User Action), the Popup window is still blocked. What am I missing here?

popup-Blocker2.gif
I have a sample solution created for you as well.

View all files for Question ID: 28630908
https://filedb.experts-exchange.com/incoming/ee-stuff/8409-BypassPopupBlocker.zip

Direct link to your file
https://filedb.experts-exchange.com/incoming/ee-stuff/8409-BypassPopupBlocker.zip
ArikkanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Silly question first . . . do you have a popup blocker on?  You can test this by holding down the Ctrl key and then clicking the button.  If that goes through then you have a popup blocker on.  

It doesn't matter whether it's a client click or not, the popup blocker will block any popup unless told not to do so for a site.
ArikkanAuthor Commented:
You know that is the issue. My company wants me to show the window even if the popup blocker is on.

Now as you can see (In my attached solution) that for the Button click, the popup opens even if popup blocker is ON.
I want the same functionality for the Treeview links.
ArikkanAuthor Commented:
I created a javascript function at front end:

    <script type="text/javascript" language="javascript">
        function openpopup(url)
        {
            window.open(url, '_blank', 'height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,titlebar=no');
        }    
    </script>


and used this code at backend:
---------------------------------------------------------------------------------------------------------------------------------------------------
-- Try 1
---------------------------------------------------------------------------------------------------------------------------------------------------
   Protected Sub tvwAccountDetails_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvwAccountDetails.SelectedNodeChanged
 
       Dim url As String = "http://www.google.com"
        Dim winFeatures As String = "toolbar=no,status=no,menubar=no,location=center,scrollbars=yes,resizable=no,height=650,width=825"
        ScriptManager.RegisterStartupScript(Me, GetType(String), "WINDOW_OPEN", String.Format("<script type='text/javascript'>var popup=window.open('{0}', 'yourWin', '{1}'); </script>", url, winFeatures), True)
    End Sub
---------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------
-- Try 2
---------------------------------------------------------------------------------------------------------------------------------------------------
  Protected Sub tvwAccountDetails_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvwAccountDetails.SelectedNodeChanged
        Dim sb As StringBuilder = New StringBuilder()
        sb.Append("<script language='javascript' type='text/javascript'> ")
        sb.Append("openpopup('" + "http://www.google.com" + "')")
        sb.Append("</script>")

        ScriptManager.RegisterStartupScript(Me, GetType(String), "WINDOW_OPEN", sb.ToString(), True)
    End Sub
---------------------------------------------------------------------------------------------------------------------------------------------------

None of these worked :-(
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Kyle AbrahamsSenior .Net DeveloperCommented:
This will work if you change the internet security zone.  You can use active directory to publish the change or try adding it to a trusted site.  I'm not positive but I *think* it would also work with the local intranet settings (again, based on the settings in the browser).

The whole point of a popup blocker is to block popups.  The only way to get around that is to make it known that you are "special" (eg: in a different zone) then the rest of the internet.  Otherwise we'd be back to the popup ads.

I'm assuming that this is for a local intranet site.

Even the user clicked the link . . . you're doing a postback which is then transferred to the server for control, so it's actually the server running the javascript.

If all you're trying to do is do a popup then you can use the onlick event on the treeview.
From: http://forums.asp.net/t/1557293.aspx?How+to+give+javascript+onclick+event+to+an+aspdotnet+treeview+parent+node+even+it+has+server+side+event+

<asp:TreeView ID="TreeView2" runat="server" ShowLines="true" onclick="clientClick();">
            <Nodes>
                <asp:TreeNode Text="123" Value="123" Selected="true" />
                <asp:TreeNode Text="qwe" Value="qwe" />
                <asp:TreeNode Text="asd" Value="asd" />
                <asp:TreeNode Text="zxc" Value="zxc" />
            </Nodes>
        </asp:TreeView>


        <script type="text/javascript">
            //change to do your popup here.
            function clientClick() {
                alert("client side clicked.");
            }


        </script>

Open in new window


Let me know if any of those work for you.
ArikkanAuthor Commented:
ok. Let me try.
ArikkanAuthor Commented:
ok sir.

So the "OnClick()" attribute seems to work even in case of popup blocker enabled.
But it will be a partial solution for me.
As this will set the same URL for the Entire <asp:TreeView....>

But, I need to open a different link for each of the <asp:Treeview...>   Nodes that I click. The value is read from the Node.
I definitely need to add something on the backend in the function:


 Protected Sub tvwAccountDetails_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
....
....
End Sub


Any way to solve this?
Kyle AbrahamsSenior .Net DeveloperCommented:
you can added a databinding to it with a parameter in your function:

something like:

 <script type="text/javascript">
            //change to do your popup here.
            function clientClick(theParam) {
               window.Navigate('page.aspx?Param=' + theParam );
            }
        </script>

<asp:TreeView ID="TreeView2" runat="server" ShowLines="true" onclick='clientClick(<%# Eval("MyDBFieldinDataTable") %>);'>
            <Nodes>
                <asp:TreeNode Text="123" Value="123" Selected="true" />
                <asp:TreeNode Text="qwe" Value="qwe" />
                <asp:TreeNode Text="asd" Value="asd" />
                <asp:TreeNode Text="zxc" Value="zxc" />
            </Nodes>
        </asp:TreeView>
ArikkanAuthor Commented:
ok. I will try it.
ArikkanAuthor Commented:
Out site has code to set session and other variables before the popup (Report) is opened.
But "Onclick" will do a client click and then server side code (set session and other variables ) will be run, giving me a blank form.

So this would not help me.

Also the Main page is using a user control with the Treeview in it.
Kyle AbrahamsSenior .Net DeveloperCommented:
That code can be moved to the report page.

You can only hold one value for the node itself . . . so presumably you're doing some kind of lookup, setting values in the session, and then redirecting to the form.

Instead, pass the value as described above in javascript, redirect to the page using your query string, redirect to the report page, and then in your report.aspx:
if (!Page.IsPostBack)
{
   //get the query string variable first, set your session variables the same way you did in the user control, and then continue on with the code.
}

Open in new window

ArikkanAuthor Commented:
:-)

I ended up doing the same yesterday.
I passed the values in the Query string and moved the code to the reports page.
ArikkanAuthor Commented:
Can you help me with few more things?

1. I want to check if the current node is a Child Node (With Value) and not an expandable node.
2. I want to pass only the selected node in the event "OnClick" on ASPX page.

Currently I hardcoded the ReportID and ValuePath variables to see if it is working.
But I need to get this from the Selected ChildNodes.

Can you guide me?


--------------------------------------------------------------------------------------------------------------------------------------------
Original Code behind - Values used in Session
--------------------------------------------------------------------------------------------------------------------------------------------
 Dim ReportId As String = tvwAccountDetails.SelectedNode.Value
 Dim ValuePath As String = tvwAccountDetails.SelectedNode.ValuePath


--------------------------------------------------------------------------------------------------------------------------------------------
ASPX page
--------------------------------------------------------------------------------------------------------------------------------------------
<asp:TreeView ID="tvwAccountDetails" runat="server" OnTreeNodePopulate="tvwAccountDetails_TreeNodePopulate" EnableClientScript="true" PopulateNodesFromClient="true" OnClick="openReport(this);">
    <Nodes>
        <asp:TreeNode Text="Account History" SelectAction="Expand" PopulateOnDemand="true"></asp:TreeNode>
    </Nodes>
</asp:TreeView>


--------------------------------------------------------------------------------------------------------------------------------------------
Javascript
--------------------------------------------------------------------------------------------------------------------------------------------
   <script language="javascript" type="text/javascript">
        function openReport(param) {
            debugger;
            if (param == undefined) { return; }
            if (param.childNodes == undefined) { return; }
            //if (param.childNodes.length > 0) {return;}
            ReportId = "600001316";
            ValuePath = "Account History/600001316";
            url = "../Reports/FormationReportsViewer.aspx?ReportId=" + ReportId + "&ValuePath=" + ValuePath + "&";
            window.open(url);
        }
    </script>
Kyle AbrahamsSenior .Net DeveloperCommented:
I've been under a stringent deadline so my responses may be a bit slow.  

The easiest way I'm thinking is to add a TreeNodeDataBoundEvent.  (https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.treenodedatabound(v=vs.110).aspx)

you can then bind the onclick directly to the node instead of the treeview itself.  See the pseudoish code below to that explains my thought process.  Let me know if you have more questions.

Dim  dt as DataTable  
protected sub tvwAccountDetails_TreeNodePopulate(ByVal Sender as Object, ByVal e as EventArgs)
   ' Execute Query
   dt = theQueryResults
end sub


protected sub tvwAccountDetails_TreeNodeDataBound(ByVal Sender as Object, ByVal e as EventArgs)

Dim node as TreeNode = Ctype(sender, TreeNode)
Dim row as DataRow 
row = findRow(node.value)   ' find the row based on the nodes value
node.Attribute.Add("onclick", "openReport('"  + FormatURL() +"')"  ' set the onclick passing in the param

'clear parent attr
if node.ParentNode isNot Nothing
    node.ParentNode.Attribute("onlclick") = ""
end if
end sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.