Thursday 1 August 2013

Automatically hyperlink URL and Email

Add namespace using System.Text.RegularExpressions; 

Html:

<asp:TextBox id="InputTextBox" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 96px"
runat="server" Height="64px" Width="472px"></asp:TextBox>
<asp:Label id="lbContent" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 208px"
runat="server" Height="99px" Width="473px" BorderStyle="Ridge" BorderColor="Transparent"></asp:Label>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 72px" runat="server"
Width="240px" Font-Bold="True" ForeColor="#ff3366">Input URL or other text here:</asp:Label>
<asp:Button id="Button1" 
            style="Z-INDEX: 104; LEFT: 376px; POSITION: absolute; TOP: 168px" runat="server"

Text="Submit" onclick="Button1_Click"></asp:Button>

Code Behind:
on Button click

 protected void Button1_Click(object sender, EventArgs e)
        {
            string strContent = InputTextBox.Text;
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
            strContent = urlregex.Replace(strContent, "<a href=\"$1\" target=\"_blank\">$1</a>");
            Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
            strContent = emailregex.Replace(strContent, "<a href=mailto:$1>$1</a>");
            lbContent.Text += "<br>" + strContent;

        }

output:



Browser back button issue after logout

Here are various ways of how one can do it:

Option #1: Set Response Cache settings in code-behind file for a page

// Code disables caching by browser.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

Option #2: Set META tag for HTTP cache settings in your ASPX page header

<META Http-Equiv="Cache-Control" Content="no-cache"/>
<META Http-Equiv="Pragma" Content="no-cache"/>
<META Http-Equiv="Expires" Content="0"/>


Option #3: Clear browser's history through JavaScript using script tag

//clears browser history and redirects url
<SCRIPT LANGUAGE="javascript">
function ClearHistory()
{
     var backlen = history.length;
     history.go(-backlen);
     window.location.href = loggedOutPageUrl
}
</SCRIPT>


Option #4: Clear browser's history through JavaScript injecting through code-behind file via Response

protected void LogOut()
{
     Session.Abandon();
     string loggedOutPageUrl = "Logout.aspx";
     Response.Write("<script language="'javascript'">");
     Response.Write("function ClearHistory()");
     Response.Write("{");
     Response.Write(" var backlen=history.length;");
     Response.Write(" history.go(-backlen);");
     Response.Write(" window.location.href='" + loggedOutPageUrl + "'; ");
     Response.Write("}");
     Response.Write("</script>");
}

Option #5: Clear browser's history through JavaScript injecting through code-behind file via Page.ClientScript

Page.ClientScript.RegisterStartupScript(this.GetType(),"clearHistory","ClearHistory();",true);


Tricky sql Query

GIVEN INPUT

Emp_Id     Emp_MgrId     Emp_Name
 1            NULL               A
 2             1                   B
 3             1                   C
 4             2                   D

EXPECTED OUTPUT

Emp_Name    Emp_MgrName
  A                     NULL
  B                        A
  C                        A
  D                        B

Solution:
Create table for query

CREATE TABLE TrickJoinQuery
(
Emp_Id INT IDENTITY(1,1) PRIMARY KEY,
Emp_MgrId INT,
Emp_Name VARCHAR(50)
)

Insert data into the table
INSERT INTO TrickJoinQuery VALUES( null,'A'),(1,'B'),(2,'C'),(3,'D')


and query is:

SELECT E.Emp_Name ,EM.Emp_Name[mgr] FROM TrickJoinQuery E
LEFT OUTER JOIN  TrickJoinQuery EM ON EM.Emp_Id = E.Emp_MgrId