Wednesday, July 27, 2011

Some Quick help

1) Make a field readonly:
txtName.Attributes.Add("readonly", "readonly")

2) Keyboard enter button is pressed in the textbox and fire an event:
This is done to ensure that when the keyboard enter button is pressed in the textbox we fire the "btnButton" click event
txtName.Attributes.Add("onkeypress", "return WebForm_FireDefaultButton(event, '" + btnButton.ClientID + "')")

3) To set the focus on a control like Textbox when the page loads.
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "focus", "setTimeout(""try{ document.getElementById('" & txtName.ClientID & "').focus(); } catch(err){}"", 1200);", True)

4) To directly access the key and value of the resource file.
lblName.Text = GetGlobalResourceObject("ABC", "Name") ////Where Main refers for a file “ABC.resx”


5) To disable controls of a form/page

// Below is the method definition
Public Sub DisableControl(ByVal ctrl As Control)
For Each subctrl As Control In ctrl.Controls
DisableControl(subctrl)
Next

If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Enabled = True
CType(ctrl, TextBox).ReadOnly = True
ElseIf TypeOf ctrl Is RadioButtonList Then
CType(ctrl, RadioButtonList).Enabled = False
ElseIf TypeOf ctrl Is CheckBox Then
CType(ctrl, CheckBox).Enabled = False
ElseIf TypeOf ctrl Is RadioButton Then
CType(ctrl, RadioButton).Enabled = False
End If
End Sub

No comments:

Post a Comment