26 June, 2008

.Net Tips and Tricks (Part -V)

Force Button Click by Pressing Enter Key
Sometimes, you will notice, that, in an ASP.Net form, depending on the circumstances, pressing the 'Enter' key to submit the form does not work.To force this to happen for a particular button on your page, just put this in the Page_Load routine:
Page.RegisterHiddenField("__EVENTTARGET", "button1")
Then, change 'button1' to the ID of your particular button. Understand, of course, if your cursor is inside of a MultiLine textbox, the default action of the enter key is to create a new line in the textbox, so, if this basically works anywhere outside of that scenario.

Select Specific DropDownList Item
With a dropdown list - you can dynamically select items in the list with a sort of 'built-in' find routine.To select a certain item, based on the Value of the item in the list:
DropDownList.SelectedIndex = DropDownList.Items.IndexOf(DropDownList.Items.FindByValue(YourValueHere))
To select a certain item, based on the Text of the item in the list:
DropDownList.SelectedIndex = DropDownList.Items.IndexOf(DropDownList.Items.FindByText("YourTextHere"))
OR - you can do it this way:
DropDownList.Items.FindByText("TextYouAreLookingFor").Selected = true
or, using the value of the item
DropDownList.Items.FindByValue("ValueYouAreLookingFor").Selected = true

ASP.Net Server Controls Not Showing on pages
It's possible that ASP.Net is not registered correctly on your system.Try running aspnet_regiis from the command prompt.Here's the default location:
C:\[Windows Folder]\Microsoft.NET\Framework\[ASP.Net Version#]\ aspnet_regiis.exe -i
Windows Server 2003, you must use aspnet_regiis -i -enable. This is because of the "Web Service Extensions" feature in IIS 6
(if you install VS.NET or the framework without IIS installed, and then go back in and install IIS afterwards, you have to re-register so that ASP.NET 'hooks' into IIS properly.")

Make a Horizontal Rule Visible or Invisible
If you want to refer to a Horizontal rule in coding, to make it visible or invisible, just use the runat=Server designation, along with an ID and you're ready to go:
[hr id="rule" visible="true" runat="server"/]

Select Specific ListBox Item
If you don't know the exact index number in the list of listbox items (listbox1.selectedindex=x), then you can find an item and select it by using the Value of the item:
ListBox1.Items.FindByValue(MyValue).Selected = true
If you don't know theValue of the item, but you know the text, you can find and select the item using the Text of the item:
ListBox1.Items.FindByText("TextYouAreLookingFor").Selected = true

List all Tables in an MS Access Database
However you decide to display the list of tables from a particular MS Access Database, just create your connection and display code in the same way you would normally do it.
The only change would be your SQL statement. Structure it this way:
SELECT [Name] FROM MSysObjects WHERE [Type] = 1

Javascript - Delete Textbox text onFocus
Let's say you have an ASP.Net TextBox server control that has the text:
"Enter Fax #"
Then, you want the text to disappear when the user clicks inside the TextBox - - here is a very simple solution.
In the Page_Load event, put:

No comments: