Cool .NET Tips and Tricks #4
By Dr. Dexter Dotnetsky
Printer - Friendly Version
Dr. Dotnetsky

Howdy! It's Dr. Dotnetsky, back again for some fun and cool ideas! I've collected a few unusual ones this time around, so I hope you like 'em.

WebServices giving you the Blues under .NET Framework 1.1?

Support for various protocols such as "GET" are disabled by default under .NET Framework 1.1. So any of your webservice calls that relied on the simplified "GET" call mechanism are mysteriously going to stop working when you upgrade your server. You can restore them, along with other specific configuration options, in the <webservices> node of your individual web.config files:

<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
<add name="HttpPostLocalhost"/>
</protocols>
< /webServices>

By the way , using "add" or "remove" with the "Documentation" element adds or removes the service help page.



Dynamically Embed ASP or other page into an ASP.NET Page

It's very easy to add the content from a Classic ASP page (or a PHP or CFM page) into your ASP.NET pages at the exact place where you want it by combining the WebRequest method with judicious use of controls such as the PlaceHolder control. The following code explains all:

<%@ Page language="C#"   %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.IO" %>
<script language="C#" runat="server">

private void Page_Load(object sender, System.EventArgs e) {
    
mainStuff.Text=
"This is the main part of your content. Below will appear the \"PageEmbed.GetPageContent\" results."; content.Controls.Add(new LiteralControl(PageEmbed.GetPageContent("http://www.yahoo.com"))); } public class PageEmbed { public static string GetPageContent(string TargetPage) { try { //is page supplied a URL? Regex objRegex = new Regex(@"^http\://[a-zA-Z0-9\-\.]+[a-zA-Z]{2,3}(/\S*)?$"); string strPath=String.Empty; if(objRegex.IsMatch(TargetPage)) { strPath = TargetPage; } // if not URL treat it as resource else { Uri RequestURI = HttpContext.Current.Request.Url; strPath = RequestURI.Scheme + "://" + RequestURI.Host + HttpContext.Current.Request.ApplicationPath + "/"; strPath += TargetPage; } // Request the page WebRequest req = (HttpWebRequest)WebRequest.Create(strPath); // Process Response WebResponse res = req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream()); return sr.ReadToEnd(); } catch(WebException Ex) { return Ex.Message; } } } </script> <body> <asp:Label id=mainStuff runat="server" /><HR> <asp:Placeholder id=content runat="server" /> </body>

How to delete Keys and Values from the Registry in a .REG Script

To delete a key from the Registry:

[-HKEY_CURRENT_USER\SomeKey] (Note the hyphen appears before the name of the KEY)

When double clicking this .reg file the key "SomeKey" will be deleted along with all string, binary or Dword values in that key.

You can delete a value from the Registry, leaving the parent KEY and all other data intact, in a .REG batch file with the following syntax:

[HKEY_LOCAL_MACHINE\SOFTWARE\YourCompany\YourKey]
" valueName"=-      (note the hyphen appears after the equals sign)

 

How to open a Modal Dialog from a parent page and bring back the entered result:

Parent.aspx:

<script language="C#" runat="server">
private void  Page_Load(object sender  ,  System.EventArgs e){
  btnOpen.Attributes.Add("onclick", "var  strReturn=window.showModalDialog('child.aspx',null,'status:no;
dialogWidth:300px;dialogHeight:150px;dialogHide:true;help:no;scroll:no');
if (strReturn != null) document.getElementById('txtValue').value=strReturn;"
); } </script> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:TextBox id="txtValue" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px"
runat="server">
</asp:TextBox> <asp:Button id="btnOpen" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 24px"
runat="server" Text="Open ">
</asp:Button> </form> </body>

Child.aspx:

<script language="C#" runat="server">
private void Page_Load(object sender  ,  System.EventArgs e){
  btnOK.Attributes.Add("onclick", "window.returnValue = document.getElementById('txtValue').value; 
window.close();"
);
btnCancel.Attributes.Add("onclick", " window.opener.focus();window.close();"); } </script> <body MS_POSITIONING="GridLayout" onload="document.all.txtValue.focus();"> <form id="Form1" method="post" runat="server"> <asp:TextBox id="txtValue" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px"
runat="server"></asp:TextBox> <asp:Button id="btnOK" style="Z-INDEX: 103; LEFT: 48px; POSITION: absolute; TOP: 56px"
runat="server" Text="Ok" Width="56px"></asp:Button> <asp:Button id="btnCancel" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 56px"
runat="server" Text="Cancel"></asp:Button> </form> </body>

When you click on the "OPEN" button in the parent.aspx page, a Modal Dialog ("Web Page Dialog") will come up with an input field and a button. The focus will be set on the input field. Upon closing the dialog, the value the user has entered is transferred to the text box in the main page.

Well, that's it for this time. Gotta catch a train, and I've got an extra large Absolut Martini (up, with two olives, thank you ) that's starting to get warm...



 

Dr. Dexter Dotnetsky is the alter-ego of the Eggheadcafe.com forums, where he often pitches in to help answer particularly difficult questions and make snide comments. Dr. Dotnetsky holds no certifications, and does not have a resume. Always the consummate gentleman, Dr. Dotnetsky can be reached at youbetcha@mindless.com.  Dr. Dotnetsky's motto: "If we were all meant to get along, there would be no people who wait for all the groceries to be rung up before starting to look for their damn checkbook."