|
|
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)<br />
{ <br />
bool bRet=true;<br />
try<br />
{<br />
row.GetOrdinal(FieldName);<br />
}<br />
catch
{<br />
bRet=false;<br />
}<br />
return bRet;<br />
}
Michael
|
|
|
|
|
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)<br />
{<br />
for(int i=0;i<sArray.Length;i++)<br />
{<br />
sArray[i]=sArray[i].ToLower();<br />
}<br />
return sArray;<br />
}
Michael
|
|
|
|
|
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)
{ window.setTimeout(ComboBox_Init,20);<br />
}<br />
else <br />
{ window.attachEvent("onload", ComboBox_Init);<br />
}
and now the ComboBox works correctly with SmartNavigation.
Michael
|
|
|
|
|
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
|
|
|
|
|
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
' Get the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
Return ResourceToString(ResName, Asm) 'call overload
End Function
Public Shared Function ResourceToString(ByVal ResName As String, ByVal Asm As [Assembly]) As String
' Resources are named using a fully qualified name.
Dim sName As String = Asm.GetName().Name + "." + ResName
Dim strm As Stream = Asm.GetManifestResourceStream(sName)
' Read the contents of the embedded file.
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
|
|
|
|
|
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"
<!--
function DefaultButton(btnID, event){
btn = findObj(btnID);
if (document.all){
if (event.keyCode == 13){
event.returnValue=false;
event.cancelBubble = true;
btn.click();
}
}
else if (document.getElementById){
if (event.which == 13){
event.returnValue=false;
event.cancelBubble = true;
btn.click();
btn.focus();
}
}
else if(document.layers){
if(event.which == 13){
event.returnValue=false;
event.cancelBubble = true;
btn.focus();
btn.click();
}
}
}
function NoDefaultButton(event){
if (document.all){
if (event.keyCode == 13){
event.cancelBubble = true;
}
}
else if (document.getElementById){
if (event.which == 13){
event.cancelBubble = true;
}
}
else if(document.layers){
if(event.which == 13){
event.cancelBubble = true;
}
}
}
function findObj(n, d) {
var p,i,x;
if(!d)
d=document;
if((p=n.indexOf("?"))>0 && parent.frames.length) {
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);
}
if(!(x=d[n])&&d.all)
x=d.all[n];
for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=findObj(n,d.layers[i].document);
if(!x && d.getElementById)
x=d.getElementById(n);
return x;
}
Michael
|
|
|
|