Wednesday, August 10, 2011

How to insert or add URL links inside the gridview?

There are many ways to insert or add links inside a grid. There are many ways to display all the pages in your site in a gridview.
This is one way,
<asp:HyperLinkField DataTextField="Modules" HeaderText="Name" NavigateUrl='<% Bind("URL") %>' />

This the second way to do this,
                                                                    <asp:TemplateField HeaderText="Name" SortExpression="Modules">
                                                                        <ItemTemplate>
                                                                            <asp:HyperLink ID="hlModules" runat="server" Text='<%# Bind("Modules") %>' />
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>

Then add this code in gridview RowCreated event,

if (e.Row.RowType == DataControlRowType.DataRow)
{
    HyperLink hl = (HyperLink)e.Row.FindControl("hlModules");
    hl.NavigateUrl = "MyPage.aspx";
}

But if you want to open the page in a new window or pop up. You need to put this code in RowDataBound event of the gridview and do like this,
           

if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("hlModules");
      switch(hl.Text)
      {
            case "FirstPage":
                  hl.NavigateUrl = "javascript:OpenFirstPage();";
            break;                       
            case "SecondPage":
                  hl.NavigateUrl = "javascript:OpenSecondPage();";
            break;                       
 
      }
}

Of course you need to create the javascript to open a pop up window like this,

<script type="text/javascript" language="javascript">
var winArray = new Array()
function OpenFirstPage()
{
if (!winArray[0] || winArray[0].closed)
      winArray[0] = window.open("FirstPage.aspx", "FirstPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
else
            winArray[0].focus();
}
function OpenSecondPage()
{
if (!winArray[0] || winArray[0].closed)
      winArray[0] = window.open("SecondPage.aspx", "SecondPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
else
            winArray[0].focus();
}
</script>

You may refer to my other post or blog on how to open popup or close all popup windows.

Sunday, August 7, 2011

How to close all windows upon signout?

This solution might not be the exact solution to close all open windows, but it does close all windows upon clicking sign out.
First, you need to create a sign out button on your master page. Then put this code your click event of the button.
Here is the code:
            Session.Clear();
            Response.Redirect("~/LoginPage.aspx");
Second, You need to create a login page. I do not need to put the code for the login page. You can do that.
Third, You need to create javascript that will open each page.
Here is the javascript:
    <script type="text/javascript" language="javascript">
        var winArray = new Array()
        function OpenFirstPage() {
            if (!winArray[0] || winArray[0].closed)
                winArray[0] = window.open("FirstPage.aspx", "FirstPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
            else
                winArray[0].focus();
        }
        function OpenSecondPage() {
            if (!winArray[1] || winArray[1].closed)
                winArray[1] = window.open("SecondPage.aspx", "SecondPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
            else
                winArray[1].focus();
        }
        function OpenThirdPage() {
            if (!winArray[2] || winArray[2].closed)
                winArray[2] = window.open("ThirdPage.aspx", "ThirdPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
            else
                winArray[2].focus();
        }
        function SignOut()
        {
            if (winArray[0] == null)
                OpenFirstPage();
            winArray[0].close();
            if (winArray[1] == null)
                OpenSecondPage();
            winArray[1].close();
            if (winArray[2] == null)
                OpenThirdPage();
            winArray[2].close();
        }
    </script>
Fourth, You need to add attributes to your sign out and button.
Here is the code:
this.lnkUser.Attributes.Add("onclick", "javascript:OpenFirstPage()");
this.lnkTeams.Attributes.Add("onclick", "javascript: OpenSecondPage()");
this.lnkVolunteers.Attributes.Add("onclick", "javascript: OpenThirdPage()");
this.lnkLogout.Attributes.Add("onclick", "javascript:SignOut()");

References used: