Web Technologies

BLOG for Web Technologies

Freewares, Free E-Books Download, SEO, Tips, Tricks, Tweaks, Latest News, .Net, PHP, ASP, ASP.Net, CSP, MS SQL Server, MySQL, Database
earnptr.com
Thursday, December 6, 2007
ASP.Net Tips & Tricks
'Src' vs 'CodeBehind' in Page Declaration

When referring to your code behind page, in your Page Declaration, if you are NOT using VS.Net, and using a text editor, use the 'Src' designation. The 'CodeBehind' designation is only relevant when using VS.Net...

Example:


<%@ Page Inherits="ClassName" src="main.vb" %>


Apply a Theme to Entire Web site (2.0)

In order to do this, you only need to set the theme in one place - your web.config file:

<system.web>
<pages theme="YourThemeName"/>
</system.web>

ASP Sessions vs. ASP.Net Sessions

Although both ASP and ASP.Net CAN support session variables, they are not share-able. We can't call .asp sessions directly into .aspx pages.


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

Confirmation Messagebox when deleting an item from the datagrid

Many Users click the delete button within the datagrid with no attention then the result the item will be deleted from the datagrid, so we can add confirmation messagebox to appear to tell the user (are you sure you want to delete this item? first of all we have to attach the javascript code for each delete button within the datagrid,we can do that in the itemdatabound method of the datagrid:


Dim deleteButton As LinkButton = e.Item.Cells(0).Controls(0)

deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete this item?')"

where the first cell in the datagrid is a linkbutton. so when the user clicks the delete button then a confiramtion message appears so if the user clicks No then nothing happens else postback will happen then it should be a server event such as a ondeletecommand that fires when the user clicks the delete button as
Sub dgPendingCompleteForms_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs) Handles dgPendingCompleteForms.DeleteCommand
'put your code here when the user presses the yes button on the confirmation meessagebox
End Sub

For ASP.net v2.0 - also look at adding an OnClientClick attribute, and assign the Javascript confirmation here


DataReader vs DataSet

Whenever you have a one-time hit of data from a database - use a DataReader - it's much faster than a DataSet - AND -- a Dataset uses a DataReader to get its data.....

If you are requesting data that is going to be used/accessed multiple times, use the DataSet - It grabs the data from the table, puts it in memory and then closes the connection


Difference between Server.Transfer and Response.Redirect

Server.Transfer processes the page from one page directly to the next page without making a round-trip back to the client's browser. This way is faster, with a little less overhead on the server. However, it does NOT update the clients url history list or current url. Response.Redirect, as expected, is used to redirect the user's browser to another page or site. It DOES perform a trip back to the client where the client's browser is actually redirected to the new page. The browser history list IS updated to reflect the new address.


DropDownList doesn't work when clicking currently selected item

Let's say you populate your dropdown list with five items.By default, the top item is 'selected' or showing, when the population finishes. Then, if you choose that item in a that is currently showing, it won't fire the onselectedindexchanged event. That's because it's not a click event. The item in the DropDownList actually needs to change to fire this event.


The way to get around this is to add a 'dummy' item to the top of the list, once it's populated, like:

" -- Choose -- "

Then, any valid item you choose from there should work. The only drawback is that you need to code around this item in your event procedure, like this:



If ddl.SelectedItem.text <> " -- Choose -- " then


'put code for event here

End if



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.enabled="True"


Gridview Paging without a Datasource Control

If you are populating your Gridview with code, and not through a DataSource control, to be able to add paging, merely add a PageIndexChanging event for the Gridview:


Protected Sub YourGridview_PageIndexChanging
(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
YourGridview.PageIndex = e.NewPageIndex
YourGridview.DataBind()
End Sub

Importing from MS Access to SQL Server
With Enterprise Manager, included with SQL Server, it makes it very easy to Import MS Access tables directly into SQL Server. However, there is one thing in particular, to which it is worth paying close attention.

If you have an autonumber field in your MS Access table, when you are Importing it into SQL Server, make sure, that when you select the table, you also click on the 'Transform' link, over to the right of the table name. Then, click on the 'Edit SQL' button. At that point, you will see the 'Create Table' script, which will be used to actually create the table in SQL Server. The section for the autonumber field will look something like:


[id] int NOT NULL,


Make sure that before you accept it, you make sure it's converted to an Identity field like this:

[id] int NOT NULL Identity,

Labels: , ,

posted by WebTeks @ 9:05 PM   0 comments
Previous Post
Archives
Links
Template by

Free Blogger Templates

BLOGGER

Subscribe in NewsGator Online Subscribe in Rojo Add to Google Add to netvibes Subscribe in Bloglines Web Developement Blogs - BlogCatalog Blog Directory Blogarama - The Blog Directory Blog Directory & Search engine Computers Blogs - Blog Top Sites Top Computers blogs