 |
 | My blog at geekswithblogs 
Wednesday, October 5, 2005 7:26 PM
|
|
|
| |
 |
 | Contains method for DataReader 
Sunday, March 6, 2005 7:53 PM
|
|
 |
I found that function Contains to check if field name exist in the record, is available for DataRow is missing for DataReader/IDataRecord.
So the following simple function can be useful.
public static bool Contains(IDataRecord row, string FieldName)
{
bool bRet=true;
try
{
row.GetOrdinal(FieldName);
}
catch //(Exception exc)
{
bRet=false;
}
return bRet;
}
Michael
|
|
|
|
| |
 |
 | Change case to string array 
Thursday, January 13, 2005 3:13 PM
|
|
 |
I found that some functions with array of strings that may be useful, but they are not in framework classes.
If someone needs all strings in string array to be in low case, the following simple function can be useful.
public static string[] ToLower(string[] sArray)
{
for(int i=0;i<sArray.Length;i++)
{
sArray[i]=sArray[i].ToLower();
}
return sArray;
}
Michael
|
|
|
|
| |
 |
 | SmartNavigation and 'onload' JavaScript 
Monday, December 6, 2004 3:29 PM
|
|
 |
I like to use SmartNavigation="true" option, but there are several problems with it.
Recently I put MetaBuilders.ComboBox[^] on the page with SmartNavigation="true" and found that after Postback ComboBox is shown as 2 different controls- text and dropdown list.
Reading source I found that rendered page has the following javascript for IE
window.attachEvent("onload", ComboBox_Init);
But the problem is that when SmartNavigation="true", postbacks do not cause onload events.
Thanks to ideas from Jim Fisher[^] and Fons Sonnemans [^] I've changed the code to the following
if(window.__smartNav!=null) //handle smartnavigation
{ window.setTimeout(ComboBox_Init,20);
}
else
{ window.attachEvent("onload", ComboBox_Init);
}
and now the ComboBox works correctly with SmartNavigation.
Michael
|
|
|
|
| |
 |
 | Sending ASP.NET page rendered content from another ASP.NET page. 
Thursday, September 23, 2004 8:30 PM
|
|
 |
My ASP.NET application used to send content of one page from another using HttpWebRequest similar to the following
Dim objResponse As HttpWebResponse
Dim objRequest As HttpWebRequest = System.Net.HttpWebRequest.Create(url)
objResponse = objRequest.GetResponse()
It worked fine, before I desided to use session object in the sending page.
I've created CookieContainer and passed sessionID, but it didn't work.
GetResponse waited for a while and returned time-out.
As I understand, ASP.Net serializes access to session and blocks re-entrance (fair enough) .
Fortunately, I found Eric Woodruff's article[^], that gave me the solution. Thanks again, Eric.
Michael
|
|
|
|
| |
 |
 | Store JavaScript as embedded resource. 
Sunday, September 19, 2004 4:06 PM
|
|
 |
I've seen many places where people use inline concatenation to create some JavaScript in ASP.NET VB
using String '&' operator or StringBuilder.Append.
The code is usually hard to read.
It's easier to save JavaScript fucntion code into a separate file with "build Action"="embedded resource" and use several reusable helper functions.
Public Shared Function StreamToString(ByVal strm As Stream) As String
If strm.CanSeek Then strm.Seek(0, SeekOrigin.Begin)
Dim myReader As New StreamReader(strm)
Dim sRet As String = myReader.ReadToEnd()
Return sRet
End Function
Public Shared Function ResourceToString(ByVal ResName As String) As String
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
Return ResourceToString(ResName, Asm) End Function
Public Shared Function ResourceToString(ByVal ResName As String, ByVal Asm As [Assembly]) As String
Dim sName As String = Asm.GetName().Name + "." + ResName
Dim strm As Stream = Asm.GetManifestResourceStream(sName)
If strm Is Nothing Then
Throw New ApplicationException("Couldn't find embedded resource " & sName)
End If
Dim sRet As String = StreamToString(strm)
Return sRet
End Function
Public Shared Function JScript(ByVal sScript As String)
Return "<script language='javascript'>" & sScript & "<" & Chr(47) & "script>"
End Function
Or even better use Eric Woodruff's Resource Server Handler Class
[^]
Michael
|
|
|
|
| |
 |
 | Default button in ASP.NET form. 
Wednesday, September 15, 2004 7:09 PM
|
|
 |
I am using my own functions to set Default Button on ASP.NET web form.
They were created based on Janus Kamp Hansen sample[^] and later using Darrell Norton's code [^].
I found that it's convinient to set the same Default Button to the container that will apply for all children- container can be WebControl or HtmlControl.
And recently I found that this approach required escaping for TextArea(otherwise Enter Key submits default button instead of entering new line in text box).
'overloaded methods for WebControl and HtmlControl are required because common parent class Control doesn't have Attributes property
Public Shared Sub DefaultButton(ByVal container As System.Web.UI.HtmlControls.HtmlControl, ByVal objDefaultButton As Control)
container.Attributes.Add("onkeydown", "DefaultButton('" & objDefaultButton.ClientID & "',event);")
container.Page.RegisterStartupScript("ForceDefaultToScript", DefaultButtonScript())
End Sub
Public Shared Sub DefaultButton(ByVal container As System.Web.UI.WebControls.WebControl, ByVal objDefaultButton As Control)
container.Attributes.Add("onkeydown", "DefaultButton('" & objDefaultButton.ClientID & "',event);")
container.Page.RegisterStartupScript("ForceDefaultToScript", DefaultButtonScript())
End Sub
'call if container has defaultButton but child (eg textarea) doesn't need it.
Public Shared Sub EscapeDefaultButton(ByRef ctrl As System.Web.UI.WebControls.WebControl)
'See also DefaultButton
ctrl.Attributes.Add("onkeydown", "NoDefaultButton(event);")
ctrl.Page.RegisterStartupScript("ForceDefaultToScript", DefaultButtonScript())
End Sub
Private Shared Function DefaultButtonScript()
'From http: Return JScript(ResourceToString("DefaultButton.js"))
End Function
In this code I am using helper functions JScript and ResourceToString [^] to access embedded resources
JS function is stored in a separate file DefaultButton.js with "build Action"="embedded resource"
<!--
Michael
|
|
|
|
 |