eWorld.UI - Matt Hawley

Ramblings of Matt

Encode HTML - Have validateRequest = True

March 15, 2004 17:14 by matthaw

A co-worker and I had a situation today in which we wanted a particular TextBox control to allow HTML. The only problem, is that validateRequest must be done across the entire website, or for the particular page. Because of these restrictions, and the fact that the UserControl being built is placed in a dynamic page for a portal, we had to research ways to allow HTML posted entries but still keep validateRequest=True.

After doing a little bit of research, I came across the idea of replacing the character representation of common HTML elements via Javascript then decoding that information with Server.HtmlDecode.

The code is as follows:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Test.WebForm1"
validateRequest="true"%>
<html>
   <body>
      <form runat="server">
         <script language="javascript">
            function encodeMyHtml(toEncode) {
               return toEncode.replace(/&/gi, '&amp;').replace(/\"/gi, '&quot;').replace(/</gi, '&lt;').replace(/>/gi, '&gt;');

            }
         </script>

         <asp:TextBox Runat="server" ID="tbEncodedText" TextMode="MultiLine" Columns="100" Rows="10" >
         <asp:Button Runat="server" ID="btnSubmit" Text="Submit My HTML" OnClick="btnSubmit_Click"/>
         <hr>
         <asp:Literal Runat="server" ID="outputHTML" />
      </form>
   </body>
</html>
Then in my code-behind I have this in my Page_Load function to add the onclick attribute:
private void Page_Load(object sender, System.EventArgs e)
{
   if(!Page.IsPostBack)
   {
      btnSubmit.Attributes.Add("onclick", "this.form." + tbEncodedText.ClientID + ".value = encodeMyHtml(this.form." + tbEncodedText.ClientID + ".value);");
   }

}
Then my button event, I have:
private void btnSubmit_Click(object sender, EventArgs e)
{
   outputHTML.Text = Server.HtmlDecode(tbEncodedText.Text);
   tbEncodedText.Text = Server.HtmlDecode(tbEncodedText.Text);
}


Overall, this provides a nice solution to not having your entire web application or page allow HTML elements.

[Previously Posted on old Weblog on July 17, 2003]



Categories: .NET
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Reading Eventlog Entries

March 15, 2004 17:12 by matthaw

I was asked by my boss the other week, in my spare time at work, to create a script or windows service that would read all entries in the Application Event Log for that day. He pointed me to a link that used VBScript and some wacky dll named "winmgmnts". This seemed like a pain, and I was like...I know you can write to Event Logs with .NET, why couldn't you read them...sure enough, you can. The following code is used to read all entries for the current day of 3 defined logs. This, obviously, can be expanded further...which is what I intend to do.


using System;
using System.Diagnostics;

public class MyClass
{
  public static void Main()
  {
    int month = DateTime.Today.Month;
    int day = DateTime.Today.Day;
    int year = DateTime.Today.Year;

    string[] logNames = new string[] {"Application", "Security", "System"};

    foreach(string log in logNames)
    {
      EventLog appLog = new EventLog(log);

      foreach(EventLogEntry entry in appLog.Entries)
      {
        if(entry.TimeGenerated.Month == month && entry.TimeGenerated.Day == day && entry.TimeGenerated.Year == year)
        {
          Console.WriteLine("-----------------------------------------");
          Console.WriteLine(entry.EntryType.ToString());
          Console.WriteLine(entry.Message);
        }
      }
      Console.ReadLine();
    }
  }
}

[Previously Posted at old Weblog on October 1, 2003]



Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

.NET Framework 1.1 SP1

March 12, 2004 16:08 by matthaw
I had downloaded XP SP2 Beta last night to install on my Virtual PC so I could try out Whidbey.  Well, after I tried running the installer for Vault, I realized I didn't have the 1.1 framework installed, so the installation couldn't continue.  I figured, why go re-download, when I know I have the .NET framework on the SP2 Beta CD.  To my suprise, it installed the 1.1 framework, as well as SP1 for the 1.1 framework.  Does anyone have any info pointing to the changelog for this SP?  I'm intrigued to find out what was fixed.

Categories: .NET
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Whidbey Timing

March 11, 2004 23:32 by matthaw
This is the best darn news I've heard all day concerning Whidbey.  I can't wait to see the new bits, even though I haven't really played with the PDC bits yet.

Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Off to DevDays

March 9, 2004 23:36 by matthaw

Well its about 2:30, and I'm packing up my laptop (well not really because I'm using it to write this blog post) to head on out of Bloomington and travel up to Chicago for DevDays tomorrow.  Glad to see that a few of us have gotten in contact about a meetup to eat/drink afterwards tomorrow.  If your heading to DevDays, Chicago - stop on by Rock Bottom Brewery afterwards!  Jeff Key, Adam Kinney, Erik Porter and myself (along with my coworker) will be there having a good time.  I'll probably be able to check my email a few times tonight, so if you wanna meet up, drop me a line and I'll give you my cell phone number.

Now... onto DevDays 2004!



Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

System.IO Woes

March 3, 2004 22:51 by matthaw

I've been working on a Web Deployer windows application the last week or so, and everything was working great until I refactored some code the other night.  In a separate thread, I'm traversing through a directory (and subdirectories) getting the size of all the files needing to be copied.  Just after that, I start traversing through that same directory, attempting to copy the file to a destination. However, after refactoring code into 1 method for all the traversing, I've been unable to copy any of the files, and am getting a "The process cannot access the file ... because it is being used by another process."

If you have any idea or could help me out, that would be great.



Categories: .NET
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

MSDN Connections

March 1, 2004 18:37 by matthaw
As Frank stated, MSDN Connections (Australia) is up and running.  However, when I tried to register, it told me that I must change my country to Australia to get into it.  Well, since I don't live in Australia, I don't want to do that, so I guess for the masses, we'll have to wait until MSDN Connections is available for the US or other countries.

Categories: .NET
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Debugging the Debugger?

February 27, 2004 06:19 by matthaw

Tonight I ran across a small problem with the C# debugger, or rather the Command Window in VS.NET.  While stepping through my application (more to come for this) to determine where a slowness was occuring while reading the registry, I tried several different commands in the Command Window.  When doing so, I think I've found a bug in how it interprets commands.

I input:

regKey.GetValue(keyValue, "").ToString()

and what I received was:

error: 'regKey.GetValue(key, "".ToString' does not exist

Its quite obvious it couldn't parse what it interpreted, only because ITS WRONG!  Anyone have any ideas?



Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Good Uses for Empty Array Declarations

February 20, 2004 17:54 by matthaw

While working on my companies new intranet, I've decided to pull some of our existing pages that are "Classic" ASP based, into the .NET realm.  In doing so, I need to gain access to our staff information from a SQL Server Database.  Since no stored procedures were ever used, and I don't have the security level to create any, I needed to build some queries that will retrieve the data I wished.

I decided that I would develop 1 Data Access Layer method that would take in a string array of departmental codes used.  This would allow my Business Logic Layer to have N number of methods that could retrieve staff data based on 1-N departments, or just retrieving all staff. Well, since I don't know all the departmental codes currently, and don't want to re-program the thing when a new department is added, I decided to make a clause in the DAL method that will execute a query with no deparment checking.

However, to do this, I needed to pass in a empty string array. To much amazement by me, the .NET framework (or maybe just C#) allowed me to do just that.  By using the code -- new string[0] {} -- I was able to compile and call the DAL method with success.  Interesting as this is, I started to think what other uses besides this do declaring empty arrays actually have.  Have you ever used this type of declaration before, if so why?



Categories: .NET
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Interesting Tidbit

February 19, 2004 23:14 by matthaw

This afternoon I decided to bring my company's intranet into the .NET world.  The current setup has an XML Document with a XSLT tranformation to build the listing of hidden/visible hiearchy.  I wanted to make the move as easy as I could, so I remembered ASP.NET contained a XML Server Control that allowed you to display XML Data transformed by XSL/XSLT.  Very cool indeed, but this isn't what I'm really stoked about.

After remembering that this site can be hit many, many, many times a day...I wanted to use Caching to store the XML document so it would be much easier on the server.  So, I decided I would make a sub-class control of the Xml control that would handle the caching for me.  Well, after digging into Reflector to determine which method I would need to override to enable caching...I stumbled across a few Cache objects when loading the XML and XSLT documents into their appropriate objects.

I was just stunned to see that this had already been thought of, good to see the ASP.NET team is on top of things!  However, I did waste about 15 min of my time by not knowing this...maybe I just overlooked some documentation that already stated this.  Either way, very cool indeedy.



Categories: .NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed


Copyright © 2000 - 2025 , Excentrics World