Tuesday, October 4, 2011

Convert HTML Table to DataSet in Asp.Net


private DataSet ConvertHTMLTablesToDataSet(string HTML)
    {
        // Declarations 
        DataSet ds = new DataSet();
        DataTable dt = null;
        DataRow dr = null;
        DataColumn dc = null;
        string TableExpression = "<table[^>]*>(.*?)</table>";
        string HeaderExpression = "<th[^>]*>(.*?)</th>";
        string RowExpression = "<tr[^>]*>(.*?)</tr>";
        string ColumnExpression = "<td[^>]*>(.*?)</td>";
        bool HeadersExist = false;
        int iCurrentColumn = 0;
        int iCurrentRow = 0;

        // Get a match for all the tables in the HTML 
        MatchCollection Tables = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

        // Loop through each table element 
        foreach (Match Table in Tables)
        {
            // Reset the current row counter and the header flag 
            iCurrentRow = 0;
            HeadersExist = false;

            // Add a new table to the DataSet 
            dt = new DataTable();

            //Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names) 
            if (Table.Value.Contains("<th"))
            {
                // Set the HeadersExist flag 
                HeadersExist = true;

                // Get a match for all the rows in the table 
                MatchCollection Headers = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

                // Loop through each header element 
                foreach (Match Header in Headers)
                {
                    dt.Columns.Add(Header.Groups[1].ToString());
                }
            }
            else
            {
                for (int iColumns = 1; iColumns <= Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase).Count; iColumns++)
                {
                    dt.Columns.Add("Column " + iColumns);
                }
            }


            //Get a match for all the rows in the table 

            MatchCollection Rows = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

            // Loop through each row element 
            foreach (Match Row in Rows)
            {
                // Only loop through the row if it isn't a header row 
                if (!(iCurrentRow == 0 && HeadersExist))
                {
                    // Create a new row and reset the current column counter 
                    dr = dt.NewRow();
                    iCurrentColumn = 0;

                    // Get a match for all the columns in the row 
                    MatchCollection Columns = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);

                    // Loop through each column element 
                    foreach (Match Column in Columns)
                    {
                        // Add the value to the DataRow 
                        dr[iCurrentColumn] = Column.Groups[1].ToString();

                        // Increase the current column  
                        iCurrentColumn++;
                    }

                    // Add the DataRow to the DataTable 
                    dt.Rows.Add(dr);

                }

                // Increase the current row counter 
                iCurrentRow++;
            }


            // Add the DataTable to the DataSet 
            ds.Tables.Add(dt);

        }

        return ds;

    }

Saturday, July 23, 2011

Remove html tags from string in Asp.net



  public static string StripHtml(string html, bool allowHarmlessTags)
    {
        if (html == null || html == string.Empty)
            return string.Empty;
        if (allowHarmlessTags)
        {
            return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);
        }

        return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
    }

Tuesday, July 19, 2011

Email Validation in ASP.net

protected  bool EvaluateIsValid()
    {
        string val = txtEmail.Text;
        string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
        Match match = Regex.Match(val.Trim(), pattern, RegexOptions.IgnoreCase);

        if (match.Success)
            return true;
        else
            return false;
    }

Thursday, July 14, 2011

Disable copy paste in asp.net textbox



<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
</asp:TextBox>
 
 

Disable Right Click -Asp.Net


<script language="JavaScript">
      // on right click appear this message
      var DisplayMsg="Right Click Disabled";
      //return message
      function clickBYIE()
      {
          if(document.all)
          {
              alert(DisplayMsg);
              return false;
          }
      }
      //return message
      function clickBYNS(e)
      {
          if (document.layers||(document.getElementById&&!document.all))
          {
              if (e.which==2||e.which==3)
              {
               alert(DisplayMsg);
               return false;
              }
          }
      }

     //set the function as per browser
      if (document.layers)
      {
          document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickBYNS;
      }
      else
      {
          document.onmouseup=clickBYNS;document.oncontextmenu=clickBYIE;
      }
      //disabled the right click on the browser
       document.oncontextmenu=new Function("return false")
  </script>

Saturday, July 2, 2011

Set Default Button in ASP.NET


<form id="form1" runat="server" defaultbutton="Button1">

   <asp:TextBox ID="Textbox1" runat="server" />
   <asp:TextBox ID="Textbox2" runat="server" />

   <asp:Button ID="Button1" runat="server" Text="Submit" />
   <asp:Button ID="Button2" runat="server" Text="Cancel" />

</form>

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...