Archive for Code

Basecode doesnt work in Firefox 3?

For those of you that miss the time saving buttons provided by Basecode can recover them by following some simple directions I’ve compiled from different sources.

Step 1:

Prevent Firefox from checking its version number before it tries to load extensions.

You can do this by adding a new preference value.

1. Point your firefox browser at the URL toabout:config“.

2. Right-click on the preferences list to bring up the contextual menu. You should see an option that says “New.” Select that, and choose “Boolean.”

3. When it asks you for the preference name, type “extensions.checkCompatibility” (without the quotes). You have to enter the name exactly. For the value, choose “false.”

4. Restart Firefox. Firefox should give you a warning that version checking is disabled. You can double-check under “Tools:Add-Ons” to make sure everything is activated once again.

Just in case anything bad happens, there’s always a backup plan. Just tell Firefox to “Reset Prefrences”, restart and you will have extension compatibility turned back on again.

This helped a lot especially in getting some of the extensions that I’m using in firefox to work again since I’ve updated to Firefox 3.

Step 2:

Disable Check Encrypted Update for extensions.

1. Open Firefox and “about:config” in the address line and hit ENTER

2. Right-click in the list and click “New/Boolean”

3. Enter the name: extension.checkUpdateSecurity

4. Set the value to false

Install Basecode and enjoy automatic formatting.

Waking Up at the Crack of Dawn

Code Snippets

Here are some code snippets I use allot, they may be useful to someone. Trying to get them into one central source I can access from everywhere. Copy & Paste for the win.

Here is the basic code for a SubRoutine, a Function, and the PageLoad Sub:

Protected Sub SubRoutineName(ByVal variable As String, Optional ByVal variable2 As Integer = 0)
‘Code
End Sub

Protected Function FunctionName(ByVal variable As String, Optional ByVal variable2 As Integer = 0) As Boolean
‘Code
Return variable3
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘Code
End Sub

Programmatically  Create a SQL Data Source:

Dim SQLDataSource1 As New SqlDataSource
SQLDataSource1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings(”ConnectionString”).ConnectionString

Programmatically  Insert with a SQL Data Source:

SQLDataSource1.InsertCommand = “INSERT INTO [Table_Name] ([Table_Field1], [Table_Field2]) VALUES (’string’,'” & variable & “‘)”
SQLDataSource1.Insert()

Programmatically  Delete with a SQL Data Source:

SQLDataSource1.DeleteCommand = “DELETE FROM [Table_Name] WHERE [Table_Field1] = ‘” & variable & “‘ AND [Table_Field2] = ’string’”
SQLDataSource1.Delete()

Programmatically  Update with a SQL Data Source:

SQLDataSource1.UpdateCommand = “UPDATE [Table_Name] SET [Table_Field1] = ’string’ WHERE [Table_Field2] = ‘” & variable & “‘”
SQLDataSource1.Update()

Programmatically  Create a  Data Source selection with a LinqToSQL Class: (requires LinqToSQL Class file)

Dim Database As New DatabaseDataContext                 ‘DatabaseDataContext is your LinqToSQL Class Context
Dim selection = From s In Database.DatabaseTable _      ‘Pick Data Table
Where s.FieldName1 = “string1″ _       ‘Where Clause
And s.FieldName2 = “string2″ _         ‘AND
Or s.FieldName3 = variable1 _          ‘OR
Order By s.FieldName Ascending _       ‘Orderby
Select c                               ‘Finally… Select

Programmatically  Assign  Data Source: (LinqToSQL)

Dim DropDownList1 As DropDownList                       ‘Can Assign to Dropdown, Listview, Gridview, Formview, Repeater, ect.
DropDownList.AppendDataBoundItems = True        ‘True or False, Append data to Items in .net Markup. If False the list will clear first.
DropDownList.DataSource = selection             ‘Assign Datasource
DropDownList.DataTextField = “FieldName1″       ‘Assign TableField to TextField for ListItems: CheckBoxList, DropDownList, Bulleted List, ect.
DropDownList.DataValueField = “FieldName2″      ‘Assign TableField to ValueField for ListItems
DropDownList.DataBind()                         ‘Bind Data

Dim ListView1 As ListView
ListView.DataSource = selection                 ‘Assign Datasource
ListView.DataBind()                             ‘Bind Data

The following were used for specific situations, but may be useful in the future.

.NET Enter Button Submit Fix for Listview:

Because .Net uses a single form to process an entire page often when a user presses enter to submit a form it will submit the first button click on the page. This is usually not the desired function on pages with multiple form fields and buttons. We can fix this by encapsulating the input fields in question inside of an <asp:Panel> </asp:Panel> set of tags and setting the “DefaultButton” attribute to the ID of our desired Submit Button.  Here is an Example…

<asp:TextBox ID=”TextBox1″ runat=”server” Text=”TextBox1″></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Buttton1″ />

<asp:Panel id=”Panel1″ runat=”server” DefaultButton=”Button1″>
<asp:TextBox ID=”TextBox2″ runat=”server” Text=”TextBox1″></asp:TextBox>
<asp:Button ID=”Button2″ runat=”server” Text=”Button2″/>
</asp:Panel>

Without the <asp:Panel> tag and the “DefaultButton” attribute any time a user presses enter white the cursor is in TextBox2 the form will submit Button1, but with the code above it will submit Button2 as would be desired in most situations.

However if we have a Listview, Formview, or any other control with an (Edit)(Insert)ItemTemplate(s) the buttons inside cannot be used as a DefaultButton in a panel, your page will crash and throw an error refering to an IButton or something.  Usually you would place the <asp:Panel> tag inside of your EditItemTemplate, but in this situation I needed the entire FormView and a few textboxs outside of the Formview. I used the folowing method to work around this issue. I created a button at the bottom of the page and used the attribute style=”display:none” to make the browser hide the button, but still render it for use as my DefaultButton. I then created a Subroutine to run for the button’s OnClick action that simply calls the FormView’s update function.

Here is the .NET psuedo Markup for the aspx page:

<asp:Panel id=”Panel1″ runat=”server” DefaultButton=”Button1″>
<!–HTML Markup–>
<!–FormView Markup–>
<!–ASP.NET Markup–>
<asp:Button ID=”Button1″ runat=”server” Text=”Text” OnClick=”SubRoutine” style=”display:none”/>
</asp:Panel>

And the Code-Behind Subroutine:

Protected Sub SubRoutine()
FormView.UpdateItem(1) ‘Boolean for Validation; 1 = yes, 0 = no
End Sub

GetFromDatabase Function:

I used the following function to pull a single database field from a table.  This function is used throught my code allot to pull specific fields for display formatting.  My SQL Data Sources were created onpage with <asp:SQLDataSource> tags, and then refered to through the code-behind page when calling the function.

Here is the function:

Protected Function GetFromDatabase(ByVal field As String, ByVal source As String) As String
Dim result As String = String.empty                                                         ‘Declares Return Variable
Dim SQLDataSource1 As SqlDataSource = page.FindControl(source)                              ‘Declare SQL DataSource
Dim dv As DataView = _
CType(SQLDataSource1.Select(DataSourceSelectArguments.Empty), System.Data.DataView) ‘Assigns Datasource to new DataView object
If dv.Table.Rows.Count >= 1 Then                                                            ‘If Dataview has more than 0 Records
Dim dr As DataRow = dv.Table.Rows(0)                                                    ‘Select the First Row
result = dr.Item(field).ToString.Trim(” “)                                              ‘Assign the field passed to the function, Trim Whitespace
End If
Return result                                                                               ‘Return the Requested Field
End Function

Thats it for now. I’m Sure I’ll have more in the future, may even clean this up some.

How to validate a Strict Doctype while using the command target=”_blank”

I wont go into all the benefits of using a Strict document type but to sum it up by declaring that the html document is “Strict,” tells the browsers to render your code in a very specific way and not to go outside of those boundaries. This causes less headaches in the long run by having to make less hacks and conditional elements for all our IE problems. It also guarantees future compatibility through declaring a standard format. You can read more about the benefits at the following link:

http://24ways.org/2005/transitional-vs-strict-markup

But there is one problem with using the Strict Doctype. It doesn’t allow the command target=”_blank”. The argument is that the average person isnt accustomed to a website opening up multiple windows. Most visitors are used to using the back and forward buttons in the browser and find it annoying, more often than not, to have to close out a window when they can “as they usually do” hit the back button.

Well regardless of the arguments its essential that we use the more respected “Strict” Doctype while still being able to validate . 356 Berea St. has a quick solution using javascript.

“In my recent post about The target attribute and opening new windows, I stated that when I am faced with no other choice but to open a link in a new window, I prefer using unobtrusive JavaScript instead of the target=”_blank” attribute. The reason is that I always use a strict doctype, which does not allow the target=”_blank” attribute.”

You can read more here:
http://www.456bereastreet.com/archive/200605/using_javascript_instead_of_target_to_open_new_windows/

Create Games For Windows and Xbox

When I first downloaded Visual Web Developer the start page had some news about making games with Visual Studio. Last night I looked into it a little and came to find out that everything you need to design games for Windows and the Xbox is completely free! From what I’ve read you can also play and debug games on your Xbox 360. How cool is that?

I’ll probably be checking this out in my free time, but it looks like I may start leaning towards programming in C# instead of VB seeing as the XNA framework is designed for C#.

Check it Out Here : http://www.xnadevelopment.com/index.shtml

--->