Skip to main content


Showing posts with the label Tips and Trics

How to Programmatically add JavaScript File to Asp.Net page?

In this post I will show you how to add JavaScript files programmatically  to your page. 

Just add this code to the Page_Init event handler on your page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Blog_Tips.aspx.cs" Inherits="Blog_Tips" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"""><htmlxmlns=""><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div>             How to Programmatically add JavaScript File to Asp.Net page?         </div></form></body></html>

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; publicpartialclass Blog_Tips…

How to restrict download of specified file types in

In this post i will show you how to restrict your web app so that .txt (whatever file extensions you want ) files can not be downloaded. Open your web.config file and register following setting in your web.config <system.web><httpHandlers><addverb="*"path="*.txt"type="System.Web.HttpForbiddenHandler"/></httpHandlers></system.web> For more details how to register handler in web.config check out this link

How To Avoid conflicts in C#

If you really want to use an identifier that clashes with a keyword, you can qualify it with the @ prefix. For instance: class class {...} // illegal class @class {...} // legalThe @ symbol doesn't form part of the identifier itself, so @myVariable is the same as myVariable. check out this example using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication7 { class Program { staticvoid Main(string[] args) { int @int = 10; double @double = 10.121; float @float = (float)(@int + @double); Console.WriteLine(@float.ToString()); Console.Read(); } } }

The null coalescing operator: ??

This is a new feature of c# 2.0. The null coalescing operator is a short cut for checking if a value is null and if so returning the value of the second operand. Kind of like an IIF. The syntax is as follows string newValue = someValue ?? "default";The first operand someValue must be a nullable type. The above code will set the value of newValue to someValue unless it's null, then it'll set it to "default". You could also use this inline: Console.WriteLine("The value is " + (someValue ?? "null")); another use return (bool)(ViewState["IsPaged"] ?? true);

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: txtFax.Attributes("OnFocus")="document.formName.txtFax.value='';"

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

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.")

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

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.