Solved

JavaScript:  Open/Close window on link click in C#

Posted on 2008-06-23
6
4,167 Views
Last Modified: 2008-07-08
Background info:

I have a form, this form has a text box for searching and a button to search.  
On button click, a new window is opened with a gridview control that populates based on the search criteria entered in the main page. This grid view has buttonfield elements that allow the user to open form documents (insurance application) pre-populated with previously entered data.  

What happens on the click event of one of these buttonfields is I call a javascript function that calls the codebehind first to set sesson variables(not using query strings at all, so all insurance application ID information is set to a session variable).  Once the session variables are set, the page redirects to the specific url of the particular referenced applcation/form.

My problem is:  I have opened an insurance application for Client "A", I then minimize this window and am back to my main window, I perform another search, a new window is populated and I open an application for Client "B", the session variables are overwritten so that Client "B's" information is now available.  If I return to Client "A's" window and hit a save button, it overwrites client "B's" information in the database as it updates based on the session variable of an application ID.  

I need to ensure that if a new client's application is opened, the previous one is closed so that it cannot overwirte the wrong client's information.  I am attaching some code, but I think the concept is the bigger picture, I may even be going about this in the wrong direction, any other suggestions would be of great help as well.  Sometimes the straight forward answer isn't the best one!
Main Page:
 

//search section

<tr>

                <td>

                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>

                </td>

                <td>

                    <asp:Button ID="btnSearch" runat="server" Text="Search By FirmName" OnClientClick="Search()" OnClick="btnSearch_Click"  /></td>

            </tr>
 
 

JavaScript on main page:

function Search()

{

    

    <%=Page.GetPostBackEventReference(btnSearch as Control) %>

    

    window.open('/Pages/FirmSearch.aspx');

    

}
 

=====================================================
 

Codebehind for search button on aspx.cs page

protected void btnSearch_Click(object sender, EventArgs e)

        {

            Session["SearchText"] = null;
 

            Session["SearchText"] = txtSearch.Text.ToString() + "%";

            Session["searchText"].ToString();

        }
 

======================================================
 
 

Window with gridview:
 

<asp:GridView ID="gvFirmSearch" runat="server" AllowPaging="True" AutoGenerateColumns="False"

        CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"

        Width="530px" OnSelectedIndexChanged="gvFirmSearch_SelectedIndexChanged" OnRowCommand="gvFirmSearch_RowCommand">

        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

        <RowStyle BackColor="#EFF3FB" />

        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

        <EditRowStyle BackColor="#2461BF" />

        <AlternatingRowStyle BackColor="White" />

        <Columns>

            <asp:ButtonField DataTextField="firmname" HeaderText="Firm Name"  CommandName="OpenFirm" />

            <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />

            <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />

            <asp:ButtonField CommandName="OpenPIR" Text="Premium Indication" />

            <asp:ButtonField CommandName="OpenNewApp" Text="New App" />

        </Columns>

    </asp:GridView>
 

=======================================

codebehind for gridview click:
 

protected void gvFirmSearch_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            string userid = gvFirmSearch.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].Text.ToString();
 

            e.CommandName.ToString();

            
 

            DataAccess.getClientIDByEmail(userid.ToString());

            DataAccess.getAppIDByClientID(Session["clientid"].ToString());

            Session["SignedIn"] = "1";

            

            if(e.CommandName.ToString() == "OpenPIR")

                Response.Redirect("/pages/PremiumIndication/PremiumIndication.aspx",false);

            else

	            if(e.CommandName.ToString() == "OpenNewApp")

                    Response.Redirect("/pages/NewApp/NewApp.aspx", false);

                else

                    if (e.CommandName.ToString() == "OpenFirm")

                        Response.Redirect("/pages/UserArea.aspx", false);

            

        }

Open in new window

0
Comment
Question by:attaylor44
  • 4
  • 2
6 Comments
 

Author Comment

by:attaylor44
ID: 21849315
I would be pleased if the click of the search button closed any previous instance of an open window before opening a new window.... this would insure that no other client applcation/forms are open (we do not need to allow the end user to view multiple accounts at a single time).
0
 
LVL 7

Expert Comment

by:60MXG
ID: 21850711
Here is how I pass the parameter to other form in my applications which is written in asp.net with C#.  
--------------------------------- Main form ----------------------------------
<html>
        <script language="javascript">
              function selectIt(x)
                     {
                           var L= 350
                           if (screen.availHeight < 800)
                           L = 100;

                           var features = " ' top=10, left= " + 1 + ", width=860, height=640, resizable=yes ' "
                           var w=window.open("editApp.aspx?id=" + x, null);
                      }
        </script>
        <form>
              <table id="tblMain">
                    <tr>
                           <td width="5%"></td>
                           <td>
                                    <asp:datagrid id="dgMain" Runat="server" OnItemCreated="dgMain_ItemCreated" .....>  -- --------------------------------I didn't type in all the code here assuming you know ----------
                                          <Columns>
                                                <asp:TemplateColumn>
                                                     <ItemTemplate>
                                                           <input type="button" id="btnClick" value="View" onclick="selectIt('<%#DataBinder.Eval(Container.DataItem, "passingParameter")%>);">
                                                     </ItemTemplate>
                                                </asp:TemplateColumn>
                                          </Columns>
                                    </asp:datagrid>
                           </td>
                    </tr>
              </table>
          </form>
</html>

----------------------------------------- Main Form End Here ------------------------------------------



---------------------------------------- editApp.aspx form that accept the passing parameter --------------------
---------------------------------------- Only the code behide is shown here ------------------------------------------

using System;
using .....;
using ...;

name space Client.Data
{
        public class editApp : Client.Data
           {
                   public string tempId;  ------- this string is going to be used to capture passing parameter
                 
                   private void Page_Load(object sender, System.EventArgs e)
                   
                   tempId  = Request.QueryString["id"];           --- id  parameter pass from the javascript

                   if (tempId.ToString() == "")
                    {
                    }
                    else
                     {
                     }
            }
}

It seems much easier that way!
0
 

Author Comment

by:attaylor44
ID: 21854730
My initial thought was that a session variable would be more secure in that if the page loads based on a query string a user could simply type in www.host.com?id=12345 and get an application form to open.  We will have many users that can log in and I want to ensure that the user has been logged in and that the id they reference belongs to them, thus I didn't really want it to be passed in a query string... am I mistaken or overlooking things?

For the most part, I just want a button click event that checks to see if a window of name "my_window" has been opened, if it has been opened, I want the code to close it before opening it again with the new information.  If it is not currently open, I want to simply open a new instance of "my_window".  

With this idea, I can have a search field and button on FormA, the user clicks the button to search, it checks to see if a window is open (not just any window, but the window with a name, a child window... something like that) and if it is, close it to ensure that the user will NOT write over existing informaiton, set session variables to reference new documentID, and open a new window.

More or less, I need to know how to have a C# page open a window that it can mantain a reference to so that the button that generates this new page can check/colse the window it created.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 7

Expert Comment

by:60MXG
ID: 21857340
           private void Page_Load(object sender, System.EventArgs e)
            {

                  if (!Page.IsPostBack)
                  {
                        Configuration.ConfigReader configReader = new ConfigReader();
                        base.SetSecurityContext(PrincipalConfig.UseSession);
                        RememberMeCookie.Write();

                        gUserProfile = new UserProfile(base.CurrentIdentity);
                        if (base.CurrentIdentity.IsAnonymous)
                        {
                              Response.Redirect("../../ManageProfile.asp");
                        }
                        else
                        {
                              base.SetSecurityContext(PrincipalConfig.UseSession);
                              RememberMeCookie.Write();
                              gUserProfile = new UserProfile(base.CurrentIdentity);
                              _MQTP = new MQTP(base.CurrentIdentity);
                              UserRecordCountMQTP = _MQTP.CheckMQTPUser();
                              if (UserRecordCountMQTP < 1)
                              {
                                    Response.Write("You do not have access to this application!");
                                    Response.End();
                              }
                              else
                              {
                                    PopulatedMQTPClass();
                                    ddlRank.Enabled = false;
                                    PopulatedSquadrons();
                                    PopulatedRank();
                                    PopulatedShift();
                              }

                        }


                  }
            }


Code the Session like I did here.  In this section of the code it shows the logic of how I handle the session.
0
 

Author Comment

by:attaylor44
ID: 21857414
After passing this by my manager, I was informed that querystrings are not a valid solution at this point. If it is found that there is no way to accomplish the open/close of a window via JavaScript management then we will revisit the querystring method.  Thank you for your suggestions.
0
 

Accepted Solution

by:
attaylor44 earned 0 total points
ID: 21869094
Anyone have more info on JavaScripting open/close window?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now