Popup keeps jumping behind calling window

I have an ASP.NET application that calls another popup window.  The code for calling it is below. It is called from a button click event.  The button itself is inside of a Gridview edititemtemplate.

The problem I'm having is that the popup pops up, but then it prompty hides behind the calling window.  How can I stop that from happening?  

Dim StartDate As TextBox = CType(GridView1.Rows.Item(index).Controls.Item(1).FindControl("txtStartDate"), TextBox)
        Dim msg As String = "window.open('PopUp.aspx?textbox=" & StartDate.ClientID & "','cal','status=no,width=200,height=180,left=270,top=220')"
        Page.ClientScript.RegisterStartupScript(GetType(String), "showcal", "<script language='javascript'>" & msg & "</script>")
Who is Participating?
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.

"<script language='javascript'>" & msg & "</script>"

"<script language='javascript'>" & msg & ";cal.focus();</script>"

tsb5270Author Commented:
I had my fingers crossed,  but it doesn't work.  The window pops up and still hides.  I also get a javascript warning that says 'cal' is undefined.
I'm a dork, sorry.

<script language='javascript'>newwindow=" & msg & ";if (window.focus) {newwindow.focus()}</script>"

This should work... works on my own pages, though I don't call it server side like you are.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

tsb5270Author Commented:
I think you're right.  The problem is because I'm calling it server side.  I'm able to get it to stay on top if I call it client side, but then I'm not able to get the date (did I mention this is a calendar control) to populate the textbox on the main window's page because the textbox is buried in a the edititemtemplate of a Gridview which is inside  a custom usercontrol.  I don't want to go into all that, but I think I step back work a little hard at figure out a method that works client side.  We'll see how that goes, and if all goes well, the points and a grade A will be yours.
Dug around in some older code...

Public Sub openPopup(ByVal Opener As system.Web.UI.WebControls.WebControl, ByVal PagePath As String)
      Dim Clientscript As String
      Clientscript = "window.open('" & PagePath & "')"
      Opener.Attributes.Add("Onclick", Clientscript)
End Sub

Then called with


<asp:button id="submitButton" Text="Submit" runat="server" />

Just tested and no focus problems.
tsb5270Author Commented:
Don't want to seem dense, but where do you call openPopup from?  The click event for the button?  I'm not getting anything now.
tsb5270Author Commented:
Okay, I finally got this to work, but the solution I came up with makes me feel like such a hacker.  This seems like disaster waiting to happen.  I'll explain what I did, and hopefully someone will offer a better, more stable solution.

The popup is a calendar.  It's sort of like the datetimepicker.  Unlike the typical windows datetimepicker, this calendar needs to be told where to put the value that was picked from the calendar.  In my case, I am working with a Gridview and using templates instead of the typical bound items.  So for each row of the Gridview there is an EditItemTemplate, in which I have textboxes, two of which are for StartDate and EndDate.  In each of the date cells I also have an HTML Image control (I tried an asp:imagebutton but as noted in earlier posts the server side behavior invariably pushed the popup calendar behind the calling form).  Anyway, the popup needs to know the id of the textbox on the calling form to send the value back to.  Unfortunately, identifying a control in a templated GridView isn't as easy as one would think.  The controls are buried, so to speak, so that in the clientid can be a rather long name.  For me, since the GridView is actually part of a usercontrol, the clientid of a particular text box looks something like this: ctl00_ContentPlaceHolder1_TimeGrid1_GridView1_ctl02_txtStartDate.

And it's different for each row of the GridView.  So the next row's textbox probably has this clientid:

Anyway, so you have to figure out the clientid.

On the aspx page my html image control looks like this then:
<img src="Picture/Calendar.gif" id="IMG1"onclick="window.open('PopUp.aspx?textbox=<%# GetTextboxControlID("lblStartDate","txtStartDate")>','cal','status=no,width=200,height=180,left=270,top=220')"/>

Notice that embedded in the onclick description a function is called that takes two arguments.  The first is the name of the control in the ItemTemplate (the label that displays when the grid is not in edit mode).  The second is the textbox control associated with that column in the EditItemTemplate for the row.

Here's the function that gets called to complete the onclick event for the html image control.

 Function GetTextBoxControlID(ByVal ctrl As String, ByVal textboxname As String) As String
        Dim index As Integer = Session("rowediting")
        Dim icnt As Integer = GridView1.Rows.Count
        Dim i As Integer
        Dim controlid As String = ""
        Dim lb As Label = New Label
            Dim x As Integer = GridView1.Rows.Item(index - 1).Controls.Count
            For i = 0 To x - 1
                lb = CType(GridView1.Rows.Item(index - 1).Controls.Item(i).FindControl(ctrl), Label)
                If Not (lb Is Nothing) Then
                    Dim pos As Integer = InStr(lb.ClientID, "_ctl")
                    controlid = Left(lb.ClientID, pos - 1) & "_ctl" & Format(icnt + 2, "00") & "_" & textboxname
                    Exit For
                End If
            controlid = "ctl00_ContentPlaceHolder1_TimeGrid1_GridView1_ctl02_" & textboxname
        End Try
        Return controlid
    End Function

I know what some of you are thinking?  "What the hell is going on here? This seems crazy!"  I couldn't agree more. But here's what I am doing (only because I couldn't think of anything else).  When the user clicks the edit button, the RowEditing event fires and I capture the index of the row and store it as a session variable.  I realized I was able to locate the Label control and get its clientid, but the textbox control seemed somewhere out of reach of the normal FindControl method.  So I find the Label control, and through trial and error, I came to the conclusion that the textbox control could be derived from the the clientid of the Label.  Hence the above function.  A few problems presented themselves along the way, however.  It seems that whenever I click the edit button the GridView1.Rows.Count property didn't include the entire grid, but only returned the row number of the row I was clicking.  And even, although my row index seemed to match my expectation, I couldn't find the label control unless decreased the value by one.  So I clicked on the 2nd row (index of 1), but I had to use 0 to find the Label control.  Believe me, I was scratching my head.  So I had to adjust it.  No big deal.  The kicker came, however, when I would click on the first row.  It would return 0.  Well, I couldn't subtract one because then the index is out of range. Screw it, I said.  I trap the error and just hard coded in what I assumed (and so far it works correctly) was going to be the clientid for the textbox.

So that's my saga.  It was exhausting, but mostly frustrating, and certainly confusing (as I'm sure anyone who's reading this is also).   I'm not happy with the solution because it seems so...so...so RIDICULOUS!!  But it works...for now.  I would welcome any other solutions to replace this mess.  I'm sure there is something better, something clearer and more concise.  Anyone?  Anyone?  Bueller?  Bueller?
PAQed with points refunded (125)

Community Support Moderator

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

From novice to tech pro — start learning today.