Showing posts with label misc. Show all posts
Showing posts with label misc. Show all posts

Sunday, January 02, 2011

Allowing HTML with iFrames and jQuery




So I’ve had a problem of displaying other people’s HTML on my page and their invalid HTML messing up my page’s formatting. So, I found a solution after digging through the Outlook Web Application (OWA) HTML to see how they handle the issue. I do it a little differently, but the idea is the same.

Basically, you have a blank page in an iFrame and you insert the encoded HTML into that blank page. Here’s how I do it:

Setup a blank.htm like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head>
<
title></title>
</
head>
<
body>
<
div id="htmlcontent"></div>
</
body>
</
html>

Okay, so now on the page you need to display the HTML, you add this:

<iframe id="htmlcontent" frameborder="0" src="/blank.htm" width="600" height="400" style="background: #fff; margin: 0; padding: 0; border: 0">
<
p>Your browser does not support iframes.</p>
</
iframe>

Now all we need to do is setup the jQuery like this:

<script language="javascript" type="text/javascript">
function showHtml(id) {
$(
"#htmlcontent").contents().find("#htmlcontent").html($('#' + id).text());
}
</
script>

Now to call the jQuery:

<a href="#" onclick="showHtml('myEncodedHtmlDiv');">Sample Link</a>
<
div id="myEncodedHtmlDiv" style="display: none">&lt;p&gt;&lt;strong&gt;Greetings from the Encoded Sample!&lt;/strong&gt;</div>

With ASP.NET, you can do Server.HTMLEncode(htmlToEncode) to encode whatever string you want. The jQuery automatically decodes it when you call the .text() inside the .html() like I did above.

If you want the HTML to show up in a modal, all you need to add is this:

<div id="htmlmsg" style="display: none" class="modal">
<
iframe id="htmlcontent" frameborder="0" src="/blank.htm" width="600" height="400" style="background: #fff; margin: 0; padding: 0; border: 0">
<
p>Your browser does not support iframes.</p>
</
iframe>
</
div>
<
div id="overlay" style="display: none"></div>

Here’s the CSS to tag along:

#overlay {position: absolute;
left: 0px;
top: 0px;
width:100%;
height:100%;
text-align:center;
z-index: 1000;
background: #eeeeee 50% 50% repeat; opacity: .80;filter:Alpha(Opacity=80);
}

.modal { z-index: 10000;
position: absolute;
top: 100px;
margin: 0 auto;
background: #fff;
border:1px solid #ccc;
padding:10px;
text-align:center
}

Now you’re done. Please comment if you have any questions. Thanks for reading!


Shout it

kick it on DotNetKicks.com

Thursday, February 04, 2010

General Tips & Tricks




I was in a meeting yesterday and one of my co-workers had an issue, while remoted into his desktop, with a window popping up on his secondary monitor. Luckily, I’ve used 2 – 4 monitors a long time and I’ve run into that same issue before. The solution?

1. Press Alt+Space
2. Press M
3. Push and hold the relevant arrow key until you see the window you want

So…after telling him how to solve that issue, I thought I’d post some other tips/tricks that I use quite a bit. Here’s another one:

Ever copy over your clipboard in Visual Studio and think, “Dang! Now I have to go copy that again”…or worse you cut it…then you get to Ctrl Z and then re-copy…HUGE pain. Well luckily for us, Visual Studio remembers your clipboard and you can take advantage of it by using Ctrl+Shift+V to cycle through your previous copies. This has saved me MANY times. NOT that you should copy and paste code...as a general rule...this is not a good thing :)

Ever have to minimize all your open windows to get to your desktop? My solution to this problem was to create a group of toolbars to categorize my apps. I leave the toolbar locked, never hidden, and always on top. When I first started using this method back in 2002, my boss at a startup called Icabob (now closed), hated it because I had the auto-hide enabled so everytime he’d try to go to the file menu, my toolbar would open. I was used to it so I didn’t have the same problem :)

Another nice thing about this is that I typically have all the same software installed on all my different machines, so I can copy and paste my root “toolbars” folder on all the machines and the shortcuts still work. Keep in mind, you only would add apps that you use quite a bit. So I don’t put my entire “All Programs” section in the toolbar. Anyhow, here’s my current toolbar setup, which is docked on the top of my screen:

image

Yes that is a GroupWise icon you see up there…so all you Outlook 2010 people (my pic is already on this page) complaining should remember there are worse things. However, we are going through an Exchange migration right now, so I’ll be joining in on the complaints soon enough I’m sure :)

Here are a couple shortcuts that I don’t see used often, but are handy:

Ctrl + Shift + Esc – Opens Task Manager

Win + Pause Break – Opens Computer Properties

Here’s a couple shortcuts specifically for VS IDE that I use a lot:

Ctrl + K + C – Comment Selected Code

Ctrl + K + U – Uncomment Selected Code

For more VS tips, check out Sara Ford’s book Visual Studio Tips 251 Ways to Improve Your Productivity

Anyhow, I thought I’d share some of the things I use daily that I’ve never seen used. I also want to post my favorite jQuery plugins, favorite Firefox add-ons, and VS plugins. I’m not sure if anyone is interested or not, but if you are, let me know!

Thanks for reading!

kick it on DotNetKicks.com

Saturday, January 30, 2010

Favorite Extension Methods




I thought I’d share some of my favorite extension methods I use regularly. I’ll go through a few extensions for MailMessage, HtmlHelper, and DirectoryEntry (for Active Directory).

MailMessage Extension (only 1)

    public static class MailMessageExtensions
{
public static void Send(this MailMessage mailMessage)
{
#if (DEBUG)
mailMessage.To.Clear();
mailMessage.To.Add(
WebConfig.FromEmail);
#endif

var sc = new SmtpClient();
sc.Send(mailMessage);
}
}

HtmlHelper (only 1)

        public static string DivSuccessMessage(this HtmlHelper html, string successMessage)
{
if (html.ViewData.ModelState.IsValid && html.ViewData["success"] != null)
return "<div class=\"success-message\">" + successMessage + "</div>";

return "";
}

I created this extension to act similar to the ValidationSummary extension. Basically it checks for ViewData[“success”] and that the model state is valid then returns the sucess message, otherwise it returns nothing.


DirectoryEntry


Need a property? Here you go, just give me the name. (I also use an enum for our AD schema too to pass in the propertyName)

public static string GetProperty(this DirectoryEntry entry, string propertyName)
{
if (entry.Properties[propertyName] == null || entry.Properties[propertyName].Value == null)
return null;

return entry.Properties[propertyName].Value.ToString();
}

Need to add a property? Done…it’ll even commit the changes.

public static void AddProperty(this DirectoryEntry entry, string propertyName, string propertyVal, bool commitChange)
{
if (entry.Properties[propertyName] == null || propertyVal == null)
return;

entry.Properties[propertyName].Value = propertyVal;
if (commitChange)
entry.CommitChanges();
}

I have a couple more for adding properties…one that adds just a property and commits without asking and then one that adds multiple properties at once.

public static void AddProperty(this DirectoryEntry entry, string propertyName, string propertyVal)
{
AddProperty(entry, propertyName, propertyVal,
true);
}

public static void AddProperties(this DirectoryEntry entry, params Func<string, string>[] values)
{
foreach (var func in values)
AddProperty(entry, func.Method.GetParameters()[
0].Name, func(null), true);
}

Here’s one to enable an account

public static void EnableAccount(this DirectoryEntry entry)
{
if (!ValidUserAccount(entry))
return;

entry.Properties[
Attributes.userAccountControl].Value = GetCurrentUserAccountControl(entry) & Flags.ADS_UF_NORMAL_ACCOUNT;
entry.CommitChanges();
}

private static int GetCurrentUserAccountControl(DirectoryEntry entry)
{
int currentVal;
return !int.TryParse(entry.Properties[Attributes.userAccountControl].Value.ToString(), out currentVal) ? 0 : currentVal;
}

private static bool ValidUserAccount(DirectoryEntry entry)
{
return entry.SchemaClassName.Equals("user", StringComparison.CurrentCultureIgnoreCase);
}

Here’s one to disable an account

public static void DisableAccount(this DirectoryEntry entry)
{
if (!ValidUserAccount(entry))
return;

entry.SetPassword(
"SomeDefaultPassword");
entry.Properties[
Attributes.userAccountControl].Value = GetCurrentUserAccountControl(entry) | Flags.ADS_UF_ACCOUNTDISABLE;
entry.CommitChanges();
}

Another one to reset password

public static void SetPassword(this DirectoryEntry entry, string password)
{
if (!ValidUserAccount(entry))
return;

entry.Invoke(
"SetPassword", new object[] {password});
entry.CommitChanges();
}

There you go…a few extensions that I use and thought I’d share. I know you all have some good ones so feel free to share.


Thanks for reading!


Shout it

kick it on DotNetKicks.com

Friday, January 08, 2010

TekPub's Mastering LINQ Challenge

I have a subscription already to Tekpub, but I thought it’d be fun to participate in the challenge. Here is my solution:

 IEnumerable<int> primes = Enumerable
.Range(1, 100)
.Where(isPrime);
 private bool isPrime(int x)
{
return x==2 || (x!=1 && !Enumerable.Range(2, x/2).Any(y => x%y == 0));
}

So there you have it. I hope Justin Etheredge doesn’t consider the isPrime a custom LINQ method. If he does, then I guess my solution would look like this:

 IEnumerable<int> primes = Enumerable
.Range(1, 100)
.Where(x => x ==
2 || (x != 1 && !Enumerable.Range(2, x / 2).Any(y => x % y == 0)));

Check out the details of this challenge from Justin Etheredg’s Post.


Check out GREAT videos on Tekpub!


Here’s my test if you want to verify it’s working.

 [Test]
public void should_return_list_of_prime_numbers()
{
IEnumerable<int> primes = Enumerable
.Range(1, 100)
.Where(isPrime);

//2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
Assert.AreEqual(25, primes.Count());
Assert.AreEqual(2, primes.ToArray()[0]);
Assert.AreEqual(31, primes.ToArray()[10]);
Assert.AreEqual(73, primes.ToArray()[20]);
Assert.AreEqual(97, primes.ToArray()[24]);
}

private bool isPrime(int x)
{
return x==2 || (x!=1 && !Enumerable.Range(2, x/2).Any(y => x%y == 0));
}

kick it on DotNetKicks.com

Related Posts Plugin for WordPress, Blogger...