25 June, 2008

.Net Tips and Tricks (Part -III)

Gridview Sorting - Only Certain Columns
To allow sorting on only certain columns in a Gridview, set the AllowSorting Property to True, but, for those columns you do NOT want to be sorted, using BoundFields, or TemplateFields, remove the SortExpression property.

Why is my button event firing twice?
The most common reason for double click events are situations in which the button markup includes an OnClick event, AND, the event handler (in your code) also has a 'Handles' statement, at the end of the event handler signature - -

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
End Sub

Templates - the Answer to a Consistent Look and Feel
Let's say you have a company, and you have several websites within that company, but you need to have a consistent look and feel for each of them.Create a website with the bare minimum, that you want to look the same in all websites, with Themes, navigation, and whatever else you want.
Then, in VS.Net 2005, click File/Export Template. That's all there is to it. The next time you want a website to look like this template, just click File/New, and choose your template (which will be under 'My Templates'

Using Response object in a Class
If you try this, straight away, you will probably receive an error message, telling you the Response object is not available in this context.
So, in order to get rid of the error message, Import System.Web, and then, change :

Add 'Select' feature to existing Button/Imagebutton in TemplateField
If you already have any kind of button, whether it be a plain button, LinkButton, or Imagebutton, in a TemplateField, within a GridView, you can add 'Select' features to it, just as if you had added a 'Select' column.
All you need to do is add 'Select' to the CommandName property. Then, anything you could do with the Select CommandField, you can do with your own TemplateField.

Force TextBox Values to Capital Letters whenever user types(Simple way)
Create a css class with the following definition and apply the cssclass to the Textbox.
.MakeCapsStyle{text-transform: uppercase;}
[asp:textbox id="id" runat="server" cssclass="MakeCapsStyle" /]

Two Databound Fields in Gridview column
Boundfields are great in the Gridview, but they do have their limitations. One is that it only has room for one bound field. So, what do you do when you want two or data fields (like First and Last names) returned to the same column?
Turn the BoundField into a TemplateField, with a label in it:
[asp:Label ID="lblname" runat="server" Text='[%# Eval("Lname") +", "+ Eval("Fname") %' /]

Quick and Dirty - Clear Gridview
If you need to completely clear a Gridview quickly, create a sub like this:
Protected Sub ClearGrid(ByVal gv As GridView)
gv.DataSource = Nothing gv.DataSourceID = Nothing gv.DataBind()
End Sub
Then, you can use it by using your Gridview ID:

Nest Master pages easily
Let's say your scenario is that you want a Master page for your business's website. However, inside that 'shell', you also want a certain design for each department of the business. All you need to do, is create the second 'Department' Master page. Then, inside the Master Directive, include a reference back to the Business Master:
[%@ Master Language="VB" MasterPageFile="~/Business.master" .....
Also, be sure to use different ContentPlaceholder ids, and remember the one caveat - if you're using Nested Masters inside VS.Net 2005, you'll need to only use the html view, since the design view does not support nesting master pages.

Server.Transfer Vs. 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.

DataFormatString does not work in Boundfield
The new Gridview control in v2.0 take over the formatting of items in the BoundFields. Therefore, using a DataFormatString by itself won't work. To change the format to, let's say, Currency, we'd need to, not only set the DataFormatString="{0:c}", but we'd need to make use of the HTMLEncode property, setting it to 'False'

Assigning ASP.Net variables to Javascript
Scenario:You have an ASP.Net variable called sCustState, that you also want to use in a Javascript function. Here's how you do make it work, within Javascript, assigning it to a Javascript variable.
var sstate;
sstate='<%#sCustState %>';

Adding a Print Button with Javascript
If you'd like to use an ASP.Net button for printing, all you need to do is :
Add this line to the Page_Load event
btnPrint.Attributes("onClick") ="javascript:window.print();"
Add a button to the page called 'btnPrint'.
Technically, that's it, but, let's say you want to do something a little fancier, using DotNet. Then, add a subroutine (let's call it 'DoPrint'):
Sub doPrint(Source as Object, E as EventArgs)
lblResults.text="Page has successfully been sent to the printer!"
End Sub
Then, add a direction to this sub, in the button's tag:

No comments: