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”

Color Picker in ASP.NET

Microsoft has confirmed a bug, saying that the attributes property of ListItem will not work in the DropDownlist(i.e Web server Control).
The alternative for this is to use HTML Server Control
So we then would do is as below:

<SELECT id="DropDownList1" name="DropDownList1" runat="server"> </SELECT>

Using this control we’ll change the background Color of each Item in DropDown.

private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!IsPostBack) { foreach(FieldInfo col in typeof( KnownColor).GetFields() ) { if (col.FieldType == typeof(KnownColor) ) { DropDownList1.Items.Add(new ListItem(col.Name ,col.Name)); } } } for (int i= 0 ;i < DropDownList1.Items.Count;i++) { DropDownList1.Items[i].Attributes.Add("style", "background-color:" + DropDownList1.Items[i].Text); } }

Make sure to Import the System.Reflections namespace, as well as the System.Drawing namespace, for this example

How to Compare Time

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

Display WeekDay and Month of Date

Dim strdate As String = "06/11/2003"
Dim dt As DateTime = DateTime.Parse(strdate)
dt.Parse(strdate)

'Assign first 3 characters of Week Day to string
Dim whichday As String = Left(WeekdayName(Weekday(dt.Date)), 3)
'Assign month to String
Dim whichmonth As String = MonthName(Month(dt))

Dim whichdate As String = dt.ToString("dd")

Dim whichyear As String = dt.ToString("yyyy")
Response.Write(whichday & " " & whichmonth & " " & whichdate & " " & whichyear & "<BR>")

Loop through all or certain type of controls on the ASP.NET Page

You can loop through all or certain type of controls on ASP.NET Page using this code. Code will loop through also those controls that are contained in some other container that Form, Panel for example.

Example of looping through all TextBoxes on Page.

[C#]
private void LoopTextBoxes (Control parent)
		{
			foreach (Control c in parent.Controls)
			{
				TextBox tb = c as TextBox;
				if (tb != null)
					//Do something with the TextBox

				if (c.HasControls())
					LoopTextBoxes(c);
			}
		}

And you can start the looping by calling:

LoopTextBoxes(Page);
[VB]
  Private Sub LoopTextBoxes(ByVal parent As Control)
        Dim c As Control
        For Each c In parent.Controls
            If c.GetType() Is GetType(TextBox) Then
                'Do something with the TextBox
            End If

            If c.HasControls Then
                LoopTextBoxes(c)
            End If
        Next
    End Sub

And in this case, start it by calling:

LoopTextBoxes(Me)

Conditional Javascript – Setting Javascript Focus – Inline Coding

One of the ‘gotchas’ about inline coding is that, within a script tag, you can not have another script tag. On the surface that sounds reasonable.

However, let’s say, you have some conditional coding, in within that coding, you want to place focus in another textbox, using Javascript. Normally, you’d probably do something like this:
response.write(“<script>document.form1.txtLname.focus();</script>”)

The problem occurs when ASP.Net reads that closing script tag. It reads it as the close of it’s main Script tag.

To get around this and not confuse ASP.Net, you can do it like this:
response.Write (“<” & “script>document.form1.txtLoanNum.focus();<” & “/script>”)

Logic Driven MetaTags

Let’s say you have some sort of ‘Stats’ page and that users need to log into it. Let’s also say that you want it to use the REFRESH Meta Tag, once they’re logged in, with all the ‘parts’ of this in one page.

Since you don’t want the login part of the form refreshing all the time, what you can do is put your Meta Tag inside an ASP Literal Control.

Once your logic has been established for whether or not you want the meta tag to be available (maybe by creating uid/pwd sessions), then make the literal control ‘Visible’.

In the Page_PreRender Event, then include logic like this:

Sub Page_PreRender(Source as Object, E as EventArgs)
if session("uid")="" and session("pwd")="" then
	literal1.visible="false"
else
	literal1.visible="True"
End If
End Sub