Ayo Softech

Tuesday 16 August 2016

Download Web Page

 Step : 1
  protected void Page_Load(object sender, EventArgs e)
    {
        WebClient w = new WebClient();
        string s = w.DownloadString("http://www.ayosoftech.com");

        // 2.
        foreach (LinkItem i in LinkFinder.Find(s))
        {
            Debug.WriteLine(i);
        }
    }
 Step : 2
    public struct LinkItem
    {
        public string Href;
        public string Text;

        public override string ToString()
        {
            return Href + "\n\t" + Text;
        }
    }
 Step : 3
    static class LinkFinder
    {
        public static List<LinkItem> Find(string file)
        {
            List<LinkItem> list = new List<LinkItem>();

            // 1.
            // Find all matches in file.
            MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)",
                RegexOptions.Singleline);

            // 2.
            // Loop over each match.
            foreach (Match m in m1)
            {
                string value = m.Groups[1].Value;
                LinkItem i = new LinkItem();

                // 3.
                // Get href attribute.
                Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
                RegexOptions.Singleline);
                if (m2.Success)
                {
                    i.Href = m2.Groups[1].Value;
                }

                // 4.
                // Remove inner tags from text.
                string t = Regex.Replace(value, @"\s*<.*?>\s*", "",
                RegexOptions.Singleline);
                i.Text = t;

                list.Add(i);
            }
            return list;
        }
    }

Thursday 11 August 2016

Dynamically change (switch) CSS file programmatically from code behind in ASP.Net

Step 1 :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link id="lnkCSS" runat="server" href = "~/CSS/Default.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server" Text="This is a Label" CssClass="label"></asp:Label>
    <hr />
    <asp:Button ID="Button1" runat="server" Text="CSS 1" OnClick="ChangeCSS" CommandArgument="CSS1.css" />
    <asp:Button ID="Button2" runat="server" Text="CSS 2" OnClick="ChangeCSS" CommandArgument="CSS2.css" />
    </form>
</body>
</html>
 
Step 2 :
Default.css
CSS1.css
CSS2.css
 
Step 3 :
protected void ChangeCSS(object sender, EventArgs e)
{
    lnkCSS.Attributes["href"] = "~/CSS/" + (sender as Button).CommandArgument;
}