Search

26 June, 2008

.Net Tips and Tricks (Part -IV)

Numeric-Only Textboxes
If you haven't heard of the built in ASP.Net Validator controls, it's time to learn. There are several, but, for this case, you would use a Regular Expression Validator. The Regular Expression for making sure only numeric data is inserted into the textbox is:
^([0-9]*\d*\d{1}?\d*)$
You'd use it in a Regular Expression Validator, in conjunction with an ASP.Net Textbox, like this:
[asp:TextBox id="txtNumber" Runat="server" /]
[asp:RegularExpressionValidator ID="vldNumber" ControlToValidate="txtNumber" Display="Dynamic" ErrorMessage="Not a number" ValidationExpression="(^([0-9]*\d*\d{1}?\d*)$)" Runat="server"/]

Get the Last Write Time of File
In order to get the last time a file (like an MS Access database, or any other file) was written, you can do the following: Use/import the System.IO namespace, then
Dim sFileWriteTime as File
Label1.Text=sFileWriteTime.GetLastWriteTime(Server.MapPath ("/path/yourMDB.mdb"))

Find Specific Field in DataSet
Sometimes, when we fill DataSet from a Query, we need to get a particular item in one of the DataTable fields for later use. In order to find a particular item, we can use the field name from the database like this, and assign it to a variable:
sOrderNum = Ds.Tables[0].Rows[0]["ordnum"]
We'd just need to have created the variable globally, then, we can use that variable (sOrderNum) easily, anywhere in the page now.

MS Access Update Problems with Parameters
A very common problem, using parameters with MS Access, in ASP.Net is that it does not update the database, but also, it doesn't return an error. The problem, most likely, is that the parameters are not in the same order as the SQL statement. Remember to put the list of parameters in the same order as they hold sql statement, including any parameters in the Clause(s) at the end of the statement.

Check for Null before inserting into Database
Here's a function to ensure a Null value gets inserted into the database.
Function CheckForNULL(strStringToCheck)
if len(trim(strStringToCheck)) = 0 then
CheckForNULL =dbNull.Value
else
CheckForNULL = strStringToCheck
end if
end function
If you have a DateTime field, without a function like this, a default date will be entered (ie 1/1/1900 12:00:00 AM), instead of null, when your insert is executed.

How to Align text in the TextBox Server Control
First, in your StyleSheet (either inline or external), create a class.
For the sake of this tip, we'll call it 'txtAlign', but the name doesn't matter.
Then, add your alignment needs to the class: text-align: right;
Lastly, inside the TextBox Server Control on your page, use the CSSClass property of the textbox to assign the CSS value to the textbox:CSSClass="txtAlign"

How to set focus on an ASP.Net TextBox Control when the page loads
Two things:
1. Give your BODY Tag and ID and include 'Runat=Server'(like - - [body id="bdyMain" runat="Server"])
2. In the Page_Load event:
if not Page.IsPostBack then
bdyMain.attributes("onload")="document.form1.TextBox1.focus();"
end if

How To work with TimeSpan Class
Dim adate As DateTime = DateTime.Parse("06/24/2008")
Dim bdate As DateTime = DateTime.Parse("06/28/2008")
Dim ts As New TimeSpan(bdate.Ticks - adate.Ticks)
Response.Write(ts.TotalDays & " ")
Response.Write(ts.TotalHours & ":" & ts.TotalMinutes & ":" & ts.TotalSeconds & ":" & ts.TotalMilliseconds)

How to Compare Time
Dim t1 As String = DateTime.Parse("4:30 PM").ToString("t")
Dim t2 As String = DateTime.Now.ToString("t")
If DateTime.Compare(t1, t2) <>
Response.Write(t1.ToString() & " is <>
Else
Response.Write(t1.ToString() & " is > than " & t2.ToString())
End If

Loop through all the textbox controls on the ASP.NET Page
You can loop through all the textbox controls on ASP.NET Page using this code.
private void LoopTextBoxes (Control parent)
{
foreach (Control c in parent.Controls)
{
TextBox tb = c as TextBox;

if (tb != null)
//Do something with the TextBox
}
}
And you can start the looping by calling:
LoopTextBoxes(Page);


Disabling a Button Until Processing is Complete
Here's the scenario - let's say you have an Insert subroutine, called 'doInsert'. You want to immediately disable the Submit button, so that the end-user won't click it multiple times, therefore, submitting the same data multiple times.
For this, use a regular HTML button, including a Runat="server" and an 'OnServerClick' event designation - like this:
[INPUT id="Button1" onclick="document.form1.Button1.disabled=true;" type="button" value="Submit - Insert Data" name="Button1" runat="server" onserverclick="doInsert"]
Then, in the very last line of the 'doInsert' subroutine, add this line:
Button1.Disabled=false

No comments:

Post a Comment