Showing posts with label Tips and Trics. Show all posts
Showing posts with label Tips and Trics. Show all posts

How To - Prevent Script Attacks

This post describes the request validation feature of ASP.NET where, by default, the application is prevented from processing unencoded HTML content submitted to the server. This request validation feature can be disabled when the application has been designed to safely process HTML data.
Applies to ASP.NET 1.1 and ASP.NET 2.0.
Check out this article

Invalid postback or callback argument. Event validation is enabled using

To fix, modify the web.config file located in the /admin/ folder. Change this line:
<pages validateRequest="false" />
<pages validateRequest="false" enableEventValidation="false" />

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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        How to Programmatically add JavaScript File to Asp.Net page?

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;

public partial class Blog_Tips : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void Page_Init(object sender, EventArgs e)
        HtmlGenericControl javascript = new HtmlGenericControl("script");
        javascript.Attributes["type"] = "text/javascript";
        javascript.Attributes["src"] = "Scripts/jquery-1.3.2.min.js";

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
       <add verb="*" path="*.txt" type="System.Web.HttpForbiddenHandler" />
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 {...}    // legal
The @ 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
       static void Main(string[] args)
           int @int = 10;
           double @double = 10.121;
           float @float = (float)(@int + @double);


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.