Feeds:
Posts
Comments

In asp.net we can’t change form’s action attribute in html markup by simply putting the page name . By default it takes default.aspx as form’s action. If you are using IIS 7 url rewriting module and want to solve canonical issue then IIS7 default behaviour creates problem such as when you type url like http://www.abc.com/ and click home link in website then it displays url as http://www.abc.com/default.aspx and this will create problem for google to rank your website.

To solve this problem you have to override FORM action attribute.

reference from code project for code

Just create a class in the app_code folder, and copy and paste the following lines:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

public class cdsnetFormActionModifier : Stream
{
    private Stream _sink;
    private long _position;
    string _url;
    public cdsnetFormActionModifier(Stream sink, string url)
    {
        _sink = sink;
        _url = "$1" + url + "$3";
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override long Length
    {
        get { return 0; }
    }

    public override long Position
    {
        get { return _position; }
        set { _position = value; }
    }

    public override long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return _sink.Seek(offset, direction);
    }

    public override void SetLength(long length)
    {
        _sink.SetLength(length);
    }

    public override void Close()
    {
        _sink.Close();
    }

    public override void Flush()
    {
        _sink.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return _sink.Read(buffer, offset, count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        string s = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
        Regex reg = new Regex("(<form.*action=\")([^\"]*)" + 
                              "(\"[^>]*>)", RegexOptions.IgnoreCase);
        Match m = reg.Match(s);
        if (m.Success)
        {
            string form = reg.Replace(m.Value, _url);
            int iform = m.Index;
            int lform = m.Length;
            s = s.Substring(0, iform) + form + s.Substring(iform + lform);
        }
        byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
        _sink.Write(yaz, 0, yaz.Length);
    }
}


after creating this class use its method to override in your pageload event of Default.aspx


protected void Page_Load(object sender, EventArgs e)
{
    this.Response.Filter = 
      new cdsnetFormActionModifier(this.Response.Filter, "your_url");
}

public string CapitalIntials(string name)
{
return new CultureInfo(“en”).TextInfo.ToTitleCase(name.ToLower());
}

Using this markup you can check whether user’s browser has javascript enabled or not

<noscript>

<meta http-equiv=”Refresh” content=”0; url=noJavascriptPage.html”>

</noscript>

If user has not enabled javasript , you can redirect him to other page

Its very common problem every web developer has to face that is re-submitting data on page refresh (F5).

You will find lots of solution whiling googling but some of them are very lengthy and some are very confusing. I found very easy and effective way of handling this problem.

Using session[] and viewstate[]

On page load take one session variable and store system current date time

if (!IsPostBack)
{
Session[“Refresh”] = Server.UrlDecode(System.DateTime.Now.ToString());
}
On Button click event check whether session and viewstate are same value or not, If same then do your login

if (Session[“Refresh”].ToString() == ViewState[“Refresh”].ToString())
{

[  Do your coding  here ]

Session[“Refresh”] = Server.UrlDecode(System.DateTime.Now.ToString());
}
else
{
response.write(“You have refreshed the page”)
}

Write one Page pre render event to assing session value in viewstate  like this
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState[“Refresh”] = Session[“Refresh”];
}

Thats all….happy coding

 

 

F:\image.jpg=exefile

disable contextmenu

For disabling right click on image use this attribute in body tag

<body oncontextmenu=”return false”>

disable image toolbar

Use this markup to disable image toolbar for all the images in the page

<meta http-equiv=”imagetoolbar” content=”no” />

For individual image use this markup

<img src=”image.jpg” gallerying=”no” />


Use this attributes to disable copying from your page

<body ondragstart=”return false” onselectstart=”return false”>

Update Panel slow down the speed and increase the page size.So think 100 times before using Update Panel

There are two alternatives of Update Panel

1) Page Method.

2) Web Service.

Here I am giving you the example of filling dropdownlist by the two methods one by one

Filling Dropdownlist by Page Method
1)Page Method
Remember Page Methods will not work in master page

Put these 3 Javascript functions and scriptmanager on Page Source

//Function 1st fro calling pagemethod
function getDegree()
{
var makelist = document.getElementById(”);
var key = makelist.options[makelist.selectedIndex].text;
PageMethods.BindCollegeDegree(key,OnComplete,OnError);
}

//Function 2nd for binding dropdown by spliting the values
function OnComplete(result)
{
var models = document.getElementById(”);
for (var count=models.options.length-1;count > -1; count–)
{
models.options[count] = null;
}
var items = result.split(‘|’);
var idValue;
var textValue;
var optionItem;
for(i = 0; i < items.length;i++)
{
textValue = items[i];
idValue = items[i];
optionItem = new Option( textValue, idValue, false, false);
models.options[i] = optionItem;
}

}

function OnError(error)

{

alert(“Error Occured”);

}

You have to use ScriptManager
In Code Behind OnPage_Load put this line to call javascript function

{

drp1st.Attributes.Add(“onchange”, “return getDegree();”);

}

Write this Method in your code behind.Replace sql query with yours

//Binding Dropdown by using Page Methods

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]

public static string BindCollegeDegree(string degree)

{

string data = “”;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString);

SqlCommand cmd = new SqlCommand(“SELECT StreamName,StreamID FROM College where DegreeId=@degree”, con);

cmd.Parameters.Add(“@degree”, SqlDbType.NChar).Value = degree.Trim();

con.Open();

try

{

SqlDataReader r = cmd.ExecuteReader();

while (r.Read())

{

data = data + r[“StreamName”].ToString().Trim() + “|”;

}

int len = data.Length;

return data.Substring(0, len – 1);

}

catch (SqlException ex) { return null; }

finally { con.Close(); }

}

2) Web Services
To use web service add asmx file.
Here I have added WebService.asmx
when press f7 on this page, WebService.cs page created, write your business login here

WebService.cs
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Data.SqlClient;
using System.Configuration;

///
/// Summary description for WebService
///
[WebService(Namespace = “http://microsoft.com/webservices/&#8221;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{

public WebService()
{

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string BindingSearchDropDown(string key)
{
string data = “”;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“ConnectionString”].ConnectionString);
if (key == “College”)
{
SqlCommand cmd = new SqlCommand(“SELECT [CourseName], CourseTypeId FROM [College_CourseTypeMaster]”, con);
con.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
data = data + r[“CourseName”].ToString().Trim() + “|”;
}
return data = data.Substring(0, data.Length – 1);
}
return (“”);
}
}

Put these functions in markup

function getModels()
{
var makelist =document.getElementById(“One”);
var key = makelist.options[makelist.selectedIndex].value;
WebService.BindingSearchDropDown(key,OnGetModelsComplete);

}

function OnGetModelsComplete(result)
{
PopulateModels(result);
}
function PopulateModels(result)
{
var models = document.getElementById(“Two”);
for (var count=models.options.length-1;count > -1; count–)
{
models.options[count] = null;
}
var items = result.split(‘|’);
var idValue;
var textValue;
var optionItem;
for(i = 0; i < items.length;i++)
{
textValue = items[i];
idValue = items[i];
optionItem = new Option( textValue,idValue, false, false);
models.options[i] = optionItem;

}
}

Add ScriptManager on this page

Thats all!
Any query or help please feel free to mail me on this Id
cooolguymca at the rate gmail.com